Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView "flashing" with white background if hardware acceleration is enabled (Android 3.0+)

I have an issue with the WebView (Android 3.0+), which the WebView always displays a white background before display my black background ("flashing"). Here is my simple test code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    WebView webView = new WebView(this);
    webView.setBackgroundColor(Color.BLACK);
    setContentView(webView);
    loadWebView(webView);
    webView.loadDataWithBaseURL("localhost://", "<html><head>" +
            "<style>body {background-color: #000}img{max-width:100%}</style></head>" +
            "<body>" +
            "<img src=\"http://developer.android.com/images/practices/actionbar-phone-splitaction.png\" />" +
            "</body></html>", 
            "text/html", "UTF-8", null);
}

I have tried many solutions to get rid of this problem, but not lucky.

PS: The problem will not appear if the hardware acceleration is turned off. Have anybody have the same problem and solved it?

Thank you.

like image 221
Tri Bui Avatar asked Feb 28 '12 03:02

Tri Bui


4 Answers

I found the most effective fix for this, first mentioned here, was to set a transparent background color after the layout has been inflated:

webView.setBackgroundColor(Color.argb(1, 0, 0, 0));

Yes, it's a total hack, but it's the only solution I've found to work well without disabling hardware acceleration.

Note that this does not work through setting the background in XML.

This has been resolved in Jellybean, although some users have reported seeing this in KitKat. Check that you have not disabled hardware acceleration, and if the problem indeed disappears, you will probably want to wrap that code in a conditional statement to only target older devices:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
    webView.setBackgroundColor(Color.argb(1, 0, 0, 0));
}
like image 142
Paul Lammertsma Avatar answered Nov 07 '22 23:11

Paul Lammertsma


I have enabled the hardware acceleration for the application and have disabled it for the activity. Additionally I set the background to null, as mentioned above. It works for me now.

Another approach (untested): set the layer type to software rendering and set the background to Color.TRANSPARENT (or 0): webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Greetz Thorsten

like image 42
user640688 Avatar answered Nov 08 '22 00:11

user640688


Had this problem on Android 4.4.4 and none of the other solutions here worked. I was messing around with onPageFinished() anyway, so might as well try somthing that should be bullet proof:

I put this in the onCreateView() of the fragment that hosts the WebView:

    webView = (WebView) v.findViewById(R.id.webView);
    webView.setVisibility(View.INVISIBLE);
    webView.setBackgroundColor(Color.argb(1, 0, 0, 0));         

    webView.setWebViewClient( new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            view.setVisibility(View.VISIBLE);
            super.onPageFinished(view, url);
        }
    });

The idea is simply to hide the WebView until the first page has loaded. However, it still gave me a small flash of white until I also added the solution provided by Paul Lammertsma, webView.setBackgroundColor(Color.argb(1, 0, 0, 0));

like image 10
Magnus Avatar answered Nov 07 '22 23:11

Magnus


It is obviously awful "feature" of Android version >3. Even official app Google Reader contains this white flash. Solution is to disable HW acceleration for the activity, where you need to use fast drawing web view.

like image 5
Jan Muller Avatar answered Nov 07 '22 23:11

Jan Muller