174

I'm struggling to create a WebView with transparent background.

webView.setBackgroundColor(0x00FFFFFF);
webView.setBackgroundDrawable(myDrawable);

Then I load a html page with

<body style="background-color:transparent;" ...

The background color of the WebView is transparent but as soon as the page is loaded, it's overwritten by a black background from the html page. This only happens on android 2.2, it works on android 2.1.

So is there something to add in the html page code to make it really transparent ?

jptsetung
  • 9,064
  • 3
  • 43
  • 55
  • 1
    I'm sorry to say, but for my problem no solution listed here worked... :=( thx anyways.. the webpage always used a white background... – cV2 Sep 06 '12 at 05:53

23 Answers23

319

This worked for me,

mWebView.setBackgroundColor(Color.TRANSPARENT);
riwnodennyk
  • 8,140
  • 4
  • 35
  • 37
scottyab
  • 23,621
  • 16
  • 94
  • 105
  • 1
    Have you really tested on os 2.2 ? – jptsetung May 08 '11 at 09:13
  • 93
    I found that this has to be called *AFTER* loading the url or data. – Mark Jul 21 '11 at 18:07
  • 11
    it doesn't work in android 3.x if you are using `android:hardwareAccelerated="true"` – Macarse Jul 28 '11 at 18:46
  • 2
    Note that on ICS(4.0.3), in addition to above comments; I had to disable "Settings -> Developer Options -> Force GPU Rendering" to get transparency to work with API level 10. – Aki Aug 28 '12 at 21:57
  • Well, HW accel dont handle alpha, and a bunch of other nice canvas related features. It makes me sad ... – opaque Sep 25 '12 at 12:14
  • 10
    ACtually I don't understand why this answer gets so many up votes. webView.setBackgroundColor(0x00000000); is exactly the same as webView.setBackgroundColor(0x00FFFFFF); the alpha value is 0x00. – jptsetung Sep 26 '12 at 22:26
  • Some code for different devices always yields randomly different result. Android or "Randroid"? – macio.Jun Mar 18 '15 at 18:46
133

At the bottom of this earlier mentioned issue there is an solution. It's a combination of 2 solutions.

webView.setBackgroundColor(Color.TRANSPARENT);
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);

When adding this code to the WebViewer after loading the url, it works (API 11+).

It even works when hardeware acceleration is ON

Community
  • 1
  • 1
Ceetn
  • 2,728
  • 2
  • 25
  • 28
  • 3
    It Works as long as the Webview is not scrollable. – Rany Ishak Nov 16 '12 at 13:14
  • I needed set background color after force software rendering on samsung S3 – neworld May 16 '13 at 10:34
  • 7
    webView.setBackgroundColor(Color.argb(1, 0, 0, 0)); if you want no background flickering on scroll – Trent Seed Jul 12 '13 at 20:22
  • @Trent I tried your method and if fixed the flicker problem on Jelly Bean, but now it displays a black background on Gingerbread. Any other suggestion? – Bitcoin Cash - ADA enthusiast Aug 28 '13 at 23:33
  • Only true way for make webview background transparent. Great solution! – a.black13 Feb 28 '14 at 13:43
  • @Trent getting the same black background issue on 4.0.4 - did you get a solution for this? – oshevans Oct 23 '14 at 19:59
  • 2
    This disables hardware acceleration for the webview ! ! This was not obvious to me (blame on me for not looking it up before using :D), and the last sentence of the answer leaves the opposite impression. This actually affects a lot of things, videos, UI transformations, scrolling, canvas etc. A possible workaround http://stackoverflow.com/a/17815574/2487876 – Kristjan Liiva Jan 09 '16 at 00:59
38

I had the same issue with 2.2 and also in 2.3. I solved the problem by giving the alpa value in html not in android. I tried many things and what I found out is setBackgroundColor(); color doesnt work with alpha value. webView.setBackgroundColor(Color.argb(128, 0, 0, 0)); will not work.

so here is my solution, worked for me.

      String webData = StringHelper.addSlashes("<!DOCTYPE html><head> <meta http-equiv=\"Content-Type\" " +
      "content=\"text/html; charset=utf-8\"> </head><body><div style=\"background-color: rgba(10,10,10,0.5); " +
      "padding: 20px; height: 260px; border-radius: 8px;\"> $$$ Content Goes Here ! $$$ </div> </body></html>");

And in Java,

    webView = (WebView) findViewById(R.id.webview);
    webView.setBackgroundColor(0);
    webView.loadData(webData, "text/html", "UTF-8");

And here is the Output screenshot below.enter image description here

Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
osayilgan
  • 5,873
  • 7
  • 47
  • 68
31

Actually it's a bug and nobody found a workaround so far. An issue has been created. The bug is still here in honeycomb.

Please star it if you think it's important : http://code.google.com/p/android/issues/detail?id=14749

jptsetung
  • 9,064
  • 3
  • 43
  • 55
22

This is how you do it:

First make your project base on 11, but in AndroidManifest set minSdkVersion to 8

android:hardwareAccelerated="false" is unnecessary, and it's incompatible with 8

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

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

For safety put this in your style:

BODY, HTML {background: transparent}

worked for me on 2.2 and 4

Ali
  • 21,572
  • 15
  • 83
  • 95
16

The most important thing was not mentioned.

The html must have a body tag with background-color set to transparent.

So the full solution would be:


HTML

    <body style="display: flex; background-color:transparent">some content</body>

Activity

    WebView wv = (WebView) findViewById(R.id.webView);
    wv.setBackgroundColor(0);
    wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    wv.loadUrl("file:///android_asset/myview.html");
Community
  • 1
  • 1
lilotop
  • 527
  • 6
  • 12
  • It's mentioned in the question. But this seems to be a nice resume of what you have to do if you want it to work on all android versions. – jptsetung Jun 30 '14 at 10:10
9

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.

duggu
  • 37,851
  • 12
  • 116
  • 113
  • I don't recommend you setLayerType to LAYER_TYPE_SOFTWARE. It drastically decreases performance especially when scrolling. – Navarr Jan 27 '14 at 16:53
8

Try webView.setBackgroundColor(0);

Andrew Podkin
  • 165
  • 1
  • 1
  • 1
    There is no difference between webView.setBackgroundColor(0x00FFFFFF); and webView.setBackgroundColor(0x00); Both are supposed to be transparent color. – jptsetung Mar 01 '11 at 08:26
7

Following code work for me, though i have multiple webviews and scrolling between them is bit sluggish.

v.setBackgroundColor(Color.TRANSPARENT);
Paint p = new Paint();
v.setLayerType(LAYER_TYPE_SOFTWARE, p); 
Umair
  • 1,206
  • 1
  • 13
  • 28
  • 3
    This works on Honeycomb, but unfortunately not on ICS. Damn. But if you use it, it is more easier to specify it in the layout file using the "android:layerType" property as this will also work well on older releases where the tag will simply be ignored. – sven Mar 01 '12 at 21:10
  • 2
    What's working even on ICS is to deactivate hardware acceleration for the whole activity using `android:hardwareAccelerated="false"` in the AndroidManifest for the `activity` element. – sven Mar 01 '12 at 22:22
  • 1
    There is unnecessarily to create Paint object. `v.setLayerType(LAYER_TYPE_SOFTWARE, null);` also will work. – Sergey Glotov Mar 12 '12 at 12:24
6

Use this

WebView myWebView = (WebView) findViewById(R.id.my_web);

myWebView.setBackgroundColor(0);
Zahid Habib
  • 715
  • 3
  • 12
  • 23
  • 0 (0x00000000) = Color.TRANSPARENT see http://developer.android.com/reference/android/graphics/Color.html#TRANSPARENT – Dan Dar3 Aug 02 '15 at 00:03
6
  • After trying everything given above. I found it doesn't matter either you specify
    webView.setBackgroundColor(Color.TRANSPARENT) before or after loadUrl() /loadData().
  • The thing that matters is you should explicitly declare android:hardwareAccelerated="false" in the manifest.

Tested on IceCream Sandwich

Gaurav Arora
  • 17,124
  • 5
  • 33
  • 44
5

Just use these lines .....

webView.loadDataWithBaseURL(null,"Hello", "text/html", "utf-8", null);
webView.setBackgroundColor(0x00000000);

And remember a point that Always set background color after loading data in webview.

Atul Bhardwaj
  • 6,647
  • 5
  • 45
  • 63
3
webView.setBackgroundColor(0x00000000);
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);

this will definitely work.. set background in XML with Editbackground. Now that background will be shown

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Shivendra
  • 1,542
  • 2
  • 22
  • 35
3

This didn't work,

android:background="@android:color/transparent"

Setting the webview background color as worked

webView.setBackgroundColor(0)

Additionally, I set window background drawable as transparent

Aziz
  • 1,976
  • 20
  • 23
2

If webview is scrollable:

  1. Add this to the Manifest:

    android:hardwareAccelerated="false"
    

OR

  1. Add the following to WebView in the layout:

    android:background="@android:color/transparent"
    android:layerType="software"
    
  2. Add the following to the parents scroll view:

    android:layerType="software"
    
krsteeve
  • 1,794
  • 4
  • 19
  • 29
Baton
  • 139
  • 1
  • 2
2

set the bg after loading the html(from quick tests it seems loading the html resets the bg color.. this is for 2.3).

if you're loading the html from data you already got, just doing a .postDelayed in which you just set the bg(to for example transparent) is enough..

Lassi Kinnunen
  • 448
  • 4
  • 10
1

Try

webView.setBackgroundColor(Color.parseColor("#EDEDED"));
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
raja
  • 2,393
  • 2
  • 22
  • 25
0

I was trying to put a transparent HTML overlay over my GL view but it has always black flickering which covers my GL view. After several days trying to get rid of this flickering I found this workaround which is acceptable for me (but a shame for android).

The problem is that I need hardware acceleration for my nice CSS animations and so webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); is not an option for me.

The trick was to put a second (empty) WebView between my GL view and the HTML overlay. This dummyWebView I told to render in SW mode, and now my HTML overlays renders smooth in HW and no more black flickering.

I don't know if this works on other devices than My Acer Iconia A700, but I hope I could help someone with this.

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        RelativeLayout layout = new RelativeLayout(getApplication());
        setContentView(layout);

        MyGlView glView = new MyGlView(this);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        dummyWebView = new WebView(this);
        dummyWebView.setLayoutParams(params);
        dummyWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        dummyWebView.loadData("", "text/plain", "utf8");
        dummyWebView.setBackgroundColor(0x00000000);

        webView = new WebView(this);
        webView.setLayoutParams(params);
        webView.loadUrl("http://10.0.21.254:5984/ui/index.html");
        webView.setBackgroundColor(0x00000000);


        layout.addView(glView);
        layout.addView(dummyWebView);
        layout.addView(webView);
    }
}
Ingemar
  • 1,638
  • 2
  • 12
  • 15
0

This worked for me. try setting the background color after the data is loaded. for that setWebViewClient on your webview object like:

    webView.setWebViewClient(new WebViewClient(){

        @Override
        public void onPageFinished(WebView view, String url)
        {
            super.onPageFinished(view, url);
            webView.setBackgroundColor(Color.BLACK);
        }
    });
Abdul Mohsin
  • 1,253
  • 1
  • 13
  • 24
0

Try out:

myWebView.setAlpha(0.2f);
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
ABN
  • 1,024
  • 13
  • 26
0

If nothing helps, then most probably you have fixed sized webView, change the width and height to wrap_content or match_parent, it should work. That worked for me when I tried to load a Gif.

Hayk
  • 31
  • 1
  • 3
0

You can user BindingAdapter like this:

Java

@BindingAdapter("setBackground")
public static void setBackground(WebView view,@ColorRes int resId) {
        view.setBackgroundColor(view.getContext().getResources().getColor(resId));
        view.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
}

XML:

<layout >

    <data>

        <import type="com.tdk.sekini.R" />


    </data>

       <WebView
            ...
            app:setBackground="@{R.color.grey_10_transparent}"/>

</layout>

Resources

<color name="grey_10_transparent">#11e6e6e6</color>
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
-2
myWebView.setAlpha(0);

is the best answer. It works!

double-beep
  • 5,031
  • 17
  • 33
  • 41