Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen for a WebView finishing loading a URL?

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?

like image 778
Janusz Avatar asked Jun 30 '10 12:06

Janusz


People also ask

How do I find my WebView URL?

String webUrl = webView. getUrl();

How can I improve my WebView performance?

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.

What is a WebView URL?

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.


2 Answers

Extend WebViewClient and call onPageFinished() as follows:

mWebView.setWebViewClient(new WebViewClient() {     public void onPageFinished(WebView view, String url) {         // do your stuff here     } }); 
like image 127
ian Avatar answered Sep 29 '22 00:09

ian


@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;              }         }     }); 
like image 24
neteinstein Avatar answered Sep 29 '22 01:09

neteinstein