I am developing an app that has a fragment to show website on the webview. For slow connection it takes times to load the website and the app gets blank for few seconds so I wan
In your onCreateView()
you need to show a ProgressBar
, since your webView.loadUrl(url)
takes time to load the internet content on your WebView
. So you need to register for a call back when your WebView
is done loading your content. You can listen like this :
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// Cancel your ProgressBar here
}
});
Inside your onPageFinished()
cancel your ProgressBar
since your WebView
has completed loading the content.
WORKING CODE
public class MainActivity extends Activity {
private String url = "https://www.google.co.in/";
ProgressDialog pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = new ProgressDialog(this);
pb.setTitle("Connecting Station");
pb.setMessage("Please Wait....");
pb.setCancelable(false);
pb.show();
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setPluginState(PluginState.ON);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
pb.dismiss();
}
});
}
}