Is There a Listener for When the Webview Displays It's Content

Is there a listener for when the WebView displays it's content?

I successfully used Richard's answer with a PictureListener for a few years, but I no longer recommend this as the best solution.

This is for two reasons:

  1. webView.setPictureListener and PictureListener are both deprecated.
  2. Using this listener will cause the WebView to allocate a new Picture often. This allocation is expensive and this can have some significant performance impacts or even cause native crashes on JellyBean MR1.

Instead I recommend creating a subclass of WebView and overriding invalidate() like so:

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

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

If you still want to use the PictureListener method, you will get better performance if you setPictureListener back to null after you are done with it.

WebView - listener when scrolling is done?

Not sure there is anything in the standard WebChromeClient or WebClient interfaces that will let you do that. You will probably need to register a custom javascript object and then use a callback in the page (unless I'm wrong there are DOM level events you can listen for) to notify you when the scroll is done.

Android Webview: Detect when rendering is finished

Finally I got the very detailed for this problem. This explains why it always returns null bitmap.

Every change in my app are triggered by a user action so I came up with a modified version that only trigger invalidate() after a touchEvent

Thank for all your help.

Android WebView renders blank/white, view doesn't update on css changes or HTML changes, animations are choppy

Android Webview : detecting when page has rendered

Rendering of a WebView can take a long time for long documents, and indeed onNewPicture has been deprecated since API 12 (Honeycomb 3.1) and returns a null picture since API level 18 (Jellybean 4.3).

I have tested for API level 17 (JB 4.2), and it still works fine. Probably works fine in API 18 too if you don't need the actual Picture details.

Please this issue on the issue tracker so we can get a non-deprecated replacement.

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
PictureListener pictureListener = new PictureListener() {
@Override
@Deprecated
public void onNewPicture(WebView view, Picture picture) {
Log.i(TAG, "Picture changed!");
}

};
webView.setPictureListener(pictureListener);
}


Related Topics



Leave a reply



Submit