Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView redraw problem when loading data

I encounter a problem of display with a webview i'm using in one of my application.

When I call loadData, the webview first display the text and then load the images of the page (standard behaviour).

Sometimes, if those images modify the text position, the old text position is not cleared and both old text and new text+image is displayed

Here a screenshot of what it looks like when the problem occurs:

webview redraw problem

It's like the webview do not redraw correctly it's content. Of course a simple invalidate() does not work...

It do no occur often but lets say one time over 20.

Code used to display the data (called outside Activity life cycle methods):

// Include the html headers to the content received from WS
String webtext = String.format(ConfigApp.HTML_BODY, wsdata.getText());
mWebView.setWebChromeClient(...);
mWebView.setWebViewClient(...);
mWebView.setBackgroundColor(0);
mWebView.loadDataWithBaseURL(null, webtext, "text/html", "UTF-8", null);

Any idea how to fix this?

like image 464
ol_v_er Avatar asked Aug 05 '11 15:08

ol_v_er


2 Answers

I've finally found a way to fix the problem.

Remove the WebView transparent background:

String webtext = String.format(ConfigApp.HTML_BODY, wsdata.getText());
mWebView.setWebChromeClient(...);
mWebView.setWebViewClient(...);
// mWebView.setBackgroundColor(0);
mWebView.loadDataWithBaseURL(null, webtext, "text/html", "UTF-8", null);

Because my WebView has a blank background it's ok for me. Maybe it can be annoying for somebody else.

For sure it's a WebView bug.

like image 133
ol_v_er Avatar answered Sep 28 '22 02:09

ol_v_er


I have frequently had sporadic problems with WebViews not displaying properly after having loaded content with loadDataWithBaseUrl. Although the WebView's height was set to wrap_content, the WebView wouldn't always resize to wrap the content. Instead the webview had a height of zero. Sometimes it would display fine and other times it would not. Sometimes the problem would only happen in a portrait orientation and landscape would work fine. I tried removing my setBackgroundColor call but that it did not help. However, I was able to get the problem to go away today by changing the layout that the WebView was in from a LinearLayout to a RelativeLayout.

like image 33
John Weidner Avatar answered Sep 28 '22 01:09

John Weidner