39

I've been working on an application which have a WebView in which a static page get loaded from the assets (Also using JavaScript). This WebView is not working in KitKat, it remains blank. I am aware of the change in rendering engine (webkit to chromium) which happened in WebView in kitkat and tried the steps for migrating, which is given in Android Developers page. But it didn't help.

In logcat I am getting an error which is thrown from the Chromium source.

W/AwContents﹕ nativeOnDraw failed; clearing to background color.

Please suggest a workaround.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
gnuanu
  • 2,252
  • 3
  • 29
  • 42
  • Please make hardware acceleration is OFF. Hardware accelerated canvas rendering is not supported in Chromium WebView. – JiTHiN Dec 19 '13 at 07:19
  • Tried disabling hardware acceleration of the WebView but it didn't work either. – gnuanu Dec 19 '13 at 08:22
  • Can you share more details about the content you are loading? Have you tried using remote debugging to inspect what content is loaded into the WebView? (https://developers.google.com/chrome-developer-tools/docs/remote-debugging#debugging-webviews) – ksasq Jan 05 '14 at 13:12
  • I'm seeing this, too. In my case, I have a small webview in a Reading Options activity that shows some sample text and allows the user to resize the font, change the background, etc. If my activity is launched from the action bar, the text renders fine (but I still see this error in logcat). But if I launch from within a PreferenceActivity, then it doesn't render the text. Inspection using remote webview debugging shows an empty page with no content () even though that's not what I'm loading via loadDataWithBaseURL. – mikejonesguy Jan 07 '14 at 19:17
  • I just found this commit: http://src.chromium.org/viewvc/chrome/trunk/src/android_webview/java/src/org/chromium/android_webview/AwContents.java?r1=257037&r2=257036&pathrev=257037 Maybe calling mSettings.setEnableSupportedHardwareAcceleratedFeatures(false) with reflection could fix it. – markostamcar Aug 26 '14 at 18:04

5 Answers5

45

In my case, in Android 4.4, I was getting a black background no matter I set what and this error message in my LogCat: nativeOnDraw failed; clearing to background color.

From Googling, it seems to be because hardware accelerated canvas rendering is not supported in Chromium WebView. I added this line to the WebView to turn off hardware accelerated canvas and now it works.

mWebview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
georgiecasey
  • 21,793
  • 11
  • 65
  • 74
  • This also sped up initialization times for me. I have a couple of webviews in a ViewPager, which would often appear as blank when switching pages and then popping into the view. Using software rendering helped a lot there. – Hannes Struß Jul 23 '14 at 16:57
  • 2
    You'd get no html5 video playing in your webview as you did turn off hardware accelerated canvas by setting LAYER_TYPE_SOFTWARE. – Ahmed Hegazy Oct 22 '14 at 11:46
  • I had the same problem and the above fix worked. However we are seeing a similar problem on Kindle Fire 4.1.1 and above and in this case the workarounds don't have any effect. I've tried using AmazonWebView and it didn't make any difference. I've posted on Amazon forums and gotten no response. Any suggestions would be very much appreciated. – Chris Fawcett Dec 30 '14 at 17:46
  • 1
    Just in case anyone else has a problem with images not appearing in Kindle Fire 4.1 and above WebView, apparently the above fix (setLayerType) caused the problem. When I remove it the images reappear. But I still need it for KitKat on regular Android devices – Chris Fawcett Jan 28 '15 at 21:59
  • I tried disabling hardware acceleration but could clearly see increase in GPU Rendering times. With hardware acceleration enabled, my rendering time was <16ms for most of the duration. After disabling it, it was constantly >25-30ms. Apart from the increased rendering time, i couldn't verify if this also has any noticeable performance issue for the user. Is there a performance tradeoff associated with this solution? For me the issue happens once in a while. – Vivek Maskara Jan 26 '18 at 23:52
6

I ran into the same issue, but I did find a workaround. All you have to do is explicitly set a CSS background for your webpage. Like so:

body {
  background: white;
}

As it turns out if you do not explicitly set a background for a webpage the WebView will fail to draw said background and you'll end up with a transparent WebView.

DataDino
  • 1,507
  • 1
  • 15
  • 30
4

This seems to be a chromium webview bug.

Here is a thread about the issue: https://jira.appcelerator.org/browse/TIMOB-16479

Apparently, the accepted answer is not a sure fix. A workaround is mentioned in the link.

Tore Rudberg
  • 1,594
  • 15
  • 16
  • Doesn't matter whether the accepted answer is a sure fix or not, but it did work for me, and still working without any issues. – gnuanu Jul 24 '14 at 05:42
  • Sure. My comment is just meant as a help for other users for whom it doesn't work. The accepted answer didn't solve the issue for me. – Tore Rudberg Aug 19 '14 at 09:30
0

The disabling of the hardware accelerator comes with heavy performance toll, In my case I found out that in Kitkat this happened to me when I was re instantiating the webview element within an activity that was finished and later restarted. After a lot of trial and error, when I added:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.webViewContainer);
layout.removeAllViews();
webview.destroy();

Just before ending the activity, it seems to have the problem solved. I haven't tested it on many devices yet but if this solution is proper, then it is better by far than disabling the hardware acceleration for KitKat for the webview.

asiop
  • 707
  • 4
  • 11
-2
package com.example.testandroid;



public class MainActivity extends ActionBarActivity {

WebView webView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    if (savedInstanceState != null)
    {
        ((WebView)findViewById(R.id.web_view)).restoreState(savedInstanceState);
    }
    else{

        webView=(WebView)findViewById(R.id.web_view);
        webView.loadUrl("http://www.google.co.in");
        webView.getSettings().getJavaScriptEnabled();
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        webView.setWebViewClient(new WebViewClient()

        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view,
                    String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }
        });
    }
}


protected void onSaveInstanceState(Bundle outState) {
    webView.saveState(outState);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

}
Raj008
  • 3,539
  • 2
  • 28
  • 26