Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when Android WebView has loaded page?

I have an app that displays a splash page and removed that splash page when a URL is loaded in WebView. The following is the relevant code we are using to remove the splash page:

browser.setWebViewClient(new BrowserClient() {
 @Override
 public void onPageFinished(WebView view, String url) {
  super.onPageFinished(view, url);
  // Work around for WebView onPageFinished called twice                
  if (flag == true) {
   browser.setVisibility(View.VISIBLE);
   splashImage.setVisibility(View.INVISIBLE);
   pageLoader.setVisibility(View.INVISIBLE);
  } else {
   flag = true;
  }
 }
});

This code works... except it is slow. The splash page takes far too long to remove, long after the webpage has loaded.

Are there any tips on how I can reliably detect when WebView has loaded a page? I've been researching this for the past few days and I can't seem to come up with anything that is reliable.

The most promising I saw is the following, but putting this code throws an error in Android Console:

@Override
public void invalidate() {
 super.invalidate();

 if (getContentHeight() > 0) {
  // WebView has displayed some content and is scrollable.
 }
}

Thanks!

EDIT: There are a lot of answers proposing onPageFinished, and even someone marking this as a duplicate with a link to solutions using onPageFinished. Folks, we already are using onPageFinished. We are looking for an alternative to onPageFinished due to how unreliable it is.

like image 968
user4951834 Avatar asked Oct 20 '25 03:10

user4951834


1 Answers

If you need than you can achieve this by loaded page progress on using this web client, you can use this also.

 webView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
                   Log.e("progress", ""+progress);
                   if (progress == 100) { //...page is fully loaded.
                     // TODO - Add whatever code you need here based on web page load completion...                  
                   } 
                }
            });
like image 70
Mohit Suthar Avatar answered Oct 22 '25 05:10

Mohit Suthar