Android setLayerType Webview

19,438

Solution 1

NOTE: The question "what API level do you build for?" is VERY different from the question "what is the minimum API level that you target?".

Given the behavior you have described, it suggests you are building with API level >= 11 and testing on a device that is API level < 11.

Because .setLayerType is only available from API level 11 onwards, building with API level >= 11 will build fine, but if you are not using compatibility tricks such as reflection or:

Compatibility.getCompatibility().setWebSettingsCache(webSettings);

...then when you test on a device that is API level <11 you will get a crash because that method is not supported. On the other hand, if you test on a device of API level >= 11 you should find it works.

Solution 2

Old question, but answering anyway incase someone else finds it:

You can call setLayerType via reflection. That way the code will run independent of OS version.

try {
    Method setLayerTypeMethod = mWebView.getClass().getMethod("setLayerType", new Class[] {int.class, Paint.class});
    setLayerTypeMethod.invoke(mWebView, new Object[] {LAYER_TYPE_SOFTWARE, null});
} catch (NoSuchMethodException e) {
    // Older OS, no HW acceleration anyway
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
}

Solution 3

below code works fine Android 3.0+ but when you try this code below android 3.0 then your app forcefully closed.

webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);

You try below code on your less then API 11.

webview.setBackgroundColor(Color.parseColor("#919191"));

Or

you can also try below code which works on all API fine.

webview.setBackgroundColor(Color.parseColor("#919191"));
if (Build.VERSION.SDK_INT >= 11) {
    webview.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
}

above code use full for me.

Share:
19,438
Abhijit
Author by

Abhijit

Android Application Engineer

Updated on June 04, 2022

Comments

  • Abhijit
    Abhijit almost 2 years

    I am trying to create a WebView dynamically using the following code:

    mWebView = new WebView(this);
    mWebView.setId(R.id.webview);
    mWebView.setVerticalScrollBarEnabled(false);
    
    mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    
    mWebView.setHorizontalScrollBarEnabled(false);
    mWebView.setWebViewClient(mWebViewClient);
    mWebView.setWebChromeClient(mWebChromeClient);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
    

    But, when I run the program, my app force quits stating an error that there is no such method as 'setLayerType'. However, when I create the Webview via the xml, there seems to be no problem:

    <WebView android:id="@+id/webview"
        android:scrollbars="none"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layerType="software" />
    

    I use the 'layertype' attribute here and the app runs fine. Can somebody please explain the discrepancy? Is there no way to set the layer type of a WebView dynamically?

  • Abhijit
    Abhijit over 12 years
    Thanks for the answer! I am currently constructing my app such that it supports android versions starting from 2.1. So, I believe the API level is 7
  • Abhijit
    Abhijit over 12 years
    Since I am using a lower API level, is there no way around this, you think?
  • Abhijit
    Abhijit over 12 years
    I was actually able to comprehend the reason after breaking my head and reading a few android pages haha. But, thank you for presenting a clear answer here. Also, I see that you are new to Stackoverflow. Welcome!
  • DroidDev
    DroidDev over 10 years
    It won't run successfully independent of OS version. please see reason and scenario in which it won't run here.
  • Anders Haglund
    Anders Haglund over 10 years
    It will run just fine on any OS version. If the method does not exist a NoSuchMethodException is thrown and catched.