Set transparent color in android webview

6.5k views Asked by At

How to set transparent color in android webview?

<div style="background-color: black" >test</div>

How can I make black color to be transparent for the whole page (like chromakey)?

4

There are 4 answers

0
Sabo On

Had the same problem with the WebView, as it behaved randomly across different OS versions. Finnlay I fixed it with this code this after the loadDataWithBaseURL() call:

if (Build.VERSION.SDK_INT >= 11) {
    webView.setBackgroundColor(0x01000000);
} else {
    webView.setBackgroundColor(0x00000000);
}

I figure this will give the device something to draw, so various caching mechanisms will not kick in. But the result is practically the same as with total transparency, as it is undetectable by average human eye.

No performance penalties noticed as well.

Tested on several devices ranging from 2.2 to 4.2.

Cheers

1
John Jared On

try this

(YourWebview).setBackgroundColor(0x00000000);
1
VaMpir On

I've found the solution. I've reimplemented OnDraw method of WebView

@Override
    protected void onDraw(android.graphics.Canvas canvas) {

    super.onDraw(canvas);

    Paint p = new Paint();

    p.setARGB(255, 0, 0, 0);
    int removeColor = p.getColor(); 

    p.setAlpha(1); // if Alpha is 0 it doesn't work. I don't know why
    p.setXfermode(new AvoidXfermode(removeColor, 0, AvoidXfermode.Mode.TARGET));

    canvas.drawPaint(p);
}
0
ghader On

you can use this

webView.setBackgroundColor(0);