How to know when WebView rendering is complete

I want to display a progress bar while loading a WebView. I hide it on OnPageFinished (), but it's too early. Web browsing still displays the image.

The WebView documentation states: "When onPageFinished () is called, the rendering image is not yet updated. To get a notification for a new image, use onNewPicture (WebView, Picture)."

However, the OnNewPicture and PictureListener interface is deprecated and deprecated. Is there any other way to find out that the rendering is complete?

+4
source share
4 answers

Unfortunately, I found that there is no event or mechanism to really know when the page is fully loaded and will finish rendering.

+2
source

I think you can add and execute a JavaScript function at the end of the body tag of your web page to call a JavaScript interface function to hide the progress bar. But beware of including JavaScript in your WebView.

+1
source

I have the same problem, but there is no way to find out the time. The only way I use is to provide enough space to complete the rendering of postDelayed ().

In my case, 200ms is enough if u doesn't use heavy content over 200K in the text.

+1
source

How to create a custom WebChromeClient and override its onReceivedIcon () or onProgressChanged () methods. OnReceivedIcon () will be launched after loading favIcon and using onProgressChanged (), you can find out about the progress of loading the webview. Hope this helps

Refer to this code snippet

webView.setWebChromeClient(new CustomWebChromeClient()); 

You can use onProgressChanged or onReceivedIcon whatever suits you.

 public class CustomWebChromeClient extends WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); if (newProgress==100) { progressBar.setVisibility(View.GONE); progressBar.setProgress(newProgress); } } @Override public void onReceivedIcon(WebView view, Bitmap icon) { // icon received here super.onReceivedIcon(view, icon); } } 
+1
source

Source: https://habr.com/ru/post/1402957/


All Articles