I have a WebView
that is loading a page from the Internet. I want to show a ProgressBar
until the loading is complete.
How do I listen for the completion of page loading of a WebView
?
String webUrl = webView. getUrl();
I read about how to increase performance of WebView by implementing Caching web resources like JS, CSS and image files. You can also static resources in your native application, and by intercepting the Resource requests you can override the default behaviour of WebView.
WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application.
Extend WebViewClient and call onPageFinished() as follows:
mWebView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { // do your stuff here } });
@ian this is not 100% accurate. If you have several iframes in a page you will have multiple onPageFinished (and onPageStarted). And if you have several redirects it may also fail. This approach solves (almost) all the problems:
boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(urlNewString); return true; } @Override public void onPageStarted(WebView view, String url) { loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Override public void onPageFinished(WebView view, String url) { if (!redirect) { loadingFinished = true; //HIDE LOADING IT HAS FINISHED } else { redirect = false; } } });
UPDATE:
According to the documentation: onPageStarted will NOT be called when the contents of an embedded frame changes, i.e. clicking a link whose target is an iframe.
I found a specific case like that on Twitter where only a pageFinished was called and messed the logic a bit. To solve that I added a scheduled task to remove loading after X seconds. This is not needed in all the other cases.
UPDATE 2:
Now with current Android WebView implementation:
boolean loadingFinished = true; boolean redirect = false; mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading( WebView view, WebResourceRequest request) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(request.getUrl().toString()); return true; } @Override public void onPageStarted( WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Override public void onPageFinished(WebView view, String url) { if (!redirect) { loadingFinished = true; //HIDE LOADING IT HAS FINISHED } else { redirect = false; } } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With