here is the easiest way to add a progress bar to your android web view.
Add a boolean field in your activity / fragment
private boolean isRedirected;
This boolean prevents web pages from being redirected due to dead links. Now you can just pass the WebView object and Url web address to this method.
private void startWebView(WebView webView,String url) { webView.setWebViewClient(new WebViewClient() { ProgressDialog progressDialog; public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); isRedirected = true; return false; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); isRedirected = false; } public void onLoadResource (WebView view, String url) { if (!isRedirected) { if (progressDialog == null) { progressDialog = new ProgressDialog(SponceredDetailsActivity.this); progressDialog.setMessage("Loading..."); progressDialog.show(); } } } public void onPageFinished(WebView view, String url) { try{ isRedirected=true; if (progressDialog.isShowing()) { progressDialog.dismiss(); progressDialog = null; } }catch(Exception exception){ exception.printStackTrace(); } } }); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl(url); }
Here, on boot, it is called onPageStarted . Here I set the boolean field to false. But when the page finishes loading, it comes to the onPageFinished method, and here the Boolean field is set to true. Sometimes, if the URL is dead, it will be redirected and it will reach onLoadResource() before onPageFinished . For this reason, it will not hide the progress bar. To prevent this, I check if (!isRedirected) on onLoadResource()
in onPageFinished() before rejecting the Progress Dialog, you can write your 10 second delay code
What is it. Happy coding :)
Md. Sajedul Karim Mar 21 '16 at 10:15 2016-03-21 10:15
source share