How to make Android Webview Background transparent in KITKAT (4.4)

Recently, I have been implementing a web application on Android. I want to make the background color of the web browser Transparent .

Search, I found that there are two lines for Webview.

newWebView.setBackgroundColor(0x00000000);                  
newWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

It works for Android version 4.0 ~ 4.3, but does not work for 4.4 KITKAT.

I got only a white background. Add, when I set the background color to black, as shown below:

newWebView.setBackgroundColor(Color.BLACK);

I also saw a white background. Does anyone know a solution?

+4
source share
2 answers

It can help you.

    webView.setBackgroundColor(0x00000000);
if (Build.VERSION.SDK_INT >= 11) webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);

this.wv.setWebViewClient(new WebViewClient()
{
    @Override
    public void onPageFinished(WebView view, String url)
    {
        view.setBackgroundColor(0x00000000);
        if (Build.VERSION.SDK_INT >= 11) view.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
    }
});

Source: http://premius.net/blog/andoid/118-android-webview-transparent-background-for-android-2-3-and-4-x.html

+4

android 6 ( ), .

            wv.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    view.setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
                    } else {
                        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                    }
                }
            });
            wv.setBackgroundResource(android.R.color.transparent);
+1

Source: https://habr.com/ru/post/1541856/


All Articles