21

I am looking for a piece of code which is able to get a transparent background in a WebView for version 4.0 and above. My code is working fine with version 2.3 but it is getting a white background when I run it on version 4.0 and 4.2. I am providing my code which is working for version 2.3, but not in 4.0 and 4.2. Please help me. Thanks in advance...

In XML:

<WebView
    android:id="@+id/webView"
    android:layout_marginTop="6dp"
    android:layout_below="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:hardwareAccelerated ="true"        
    />

In activity file:

webView = (WebView) findViewById(R.id.webView);
backButton = (ImageView) findViewById(R.id.backButton);
webView.setBackgroundColor(0);                      
webView.loadUrl("file:///android_asset/Info.html");
webView.setBackgroundColor(Color.TRANSPARENT);  
/* this is used to make the background white after loading the file on screen. */
Aletheios
  • 3,930
  • 2
  • 31
  • 45
Tapesh
  • 374
  • 1
  • 6
  • 17
  • I tried this once, but eventually I used the same background for WebView and the application, so there was an illusion that it was transparent. – vortexwolf Mar 21 '13 at 15:26

3 Answers3

41

Try this:

webView.setBackgroundColor(0x00000000);

and remove android:hardwareAccelerated ="true"
as that is off-topic.

gunar
  • 14,580
  • 7
  • 55
  • 86
11

If webview is scrolable:

Add this to the Manifest:

android:hardwareAccelerated="false"

OR

Add the following to WebView in the layout:

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

Add the following to the parents scroll view:

android:layerType="software"
rds
  • 25,295
  • 17
  • 104
  • 127
Baton
  • 139
  • 1
  • 2
  • 1
    settings android:hardwareAccelerated="false" This works for me .. on android 16 HCL device.. you can enable it on application and just disable it where you use activity that has webview .. – Maher Abuthraa Jan 12 '16 at 11:32
1

setBackgroundColor(0x00000000); will make your webview transparent

VishalKale
  • 726
  • 5
  • 21