VaMpir
VaMpir

Reputation: 61

Set transparent color in android webview

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)?

Upvotes: 2

Views: 6557

Answers (4)

Sabo
Sabo

Reputation: 1677

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

Upvotes: 8

ghader
ghader

Reputation: 379

you can use this

webView.setBackgroundColor(0);

Upvotes: 0

VaMpir
VaMpir

Reputation: 61

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);
}

Upvotes: 1

John Jared
John Jared

Reputation: 800

try this

(YourWebview).setBackgroundColor(0x00000000);

Upvotes: 2

Related Questions