66

I am adding to my layout a WebView to display justified text. I want to set the background of the WebView to be transparent to appear like a textView. Here's what I did:

WebView synopsis;
synopsis=(WebView)findViewById(R.id.synopsis);
synopsis.setBackgroundColor(0x00000000);

It works on the emulator, but when I run the application on my device it doesn't work: what I get is a white background.

 String textTitleStyling = "<head><style>* {margin:0;padding:0;font-size:20; text-align:justify; color:#FFFFFF;}</style></head>";
 String titleWithStyle = textTitleStyling + "<body><h1>" + movie.synopsis + "</h1></body>";
 synopsis.loadData(textTitleStyling + movie.synopsis, "text/html", "utf-8");
 synopsis = (WebView) findViewById(R.id.synopsis);
 synopsis.getSettings();
 synopsis.setBackgroundColor(0);
PoorBob
  • 413
  • 4
  • 16
Vervatovskis
  • 2,277
  • 5
  • 29
  • 46

9 Answers9

112

Try setting the background like this:

WebView synopsis;
synopsis=(WebView)findViewById(R.id.synopsis);
synopsis.setBackgroundColor(Color.TRANSPARENT);
Paul Lefebvre
  • 6,253
  • 3
  • 28
  • 36
Rookie
  • 8,660
  • 17
  • 58
  • 91
33

try below code hope use full for you:-

webview.setBackgroundColor(Color.parseColor("#919191"));

grey code : #919191

duggu
  • 37,851
  • 12
  • 116
  • 113
20

You must put this in the XML code :

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

for your web view like this for example :

<WebView
    android:id="@+id/MyWebView"
    android:layout_width="fill_parent"
    android:layout_height="62dp"
    android:background="@android:color/transparent"
    android:scrollbars="none" />

and after this you must go to Java code and write this before loadUrl :

yourWebView.setBackgroundColor(Color.TRANSPARENT);
stkent
  • 19,772
  • 14
  • 85
  • 111
Oubaida AlQuraan
  • 1,706
  • 1
  • 18
  • 19
  • 17
    I didn't need to put android:background="@android:color/transparent" in my XML, it was enough to just do setBackgroundColor(Color.TRANSPARENT); in the code. (Only changing the XML did not work for me) – nibarius Feb 16 '15 at 06:12
  • 1
    If using Xamarin, `webview.SetBackgroundColor (Android.Graphics.Color.Transparent);` is enough. – Vahid Amiri Jan 16 '16 at 08:28
3

This is the only way I could get it to work and not load an initial white background first, if I had dark mode on:

webView.setBackgroundColor(Color.TRANSPARENT);
webView.setVisibility(View.VISIBLE);


<WebView
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="invisible"
   />
live-love
  • 48,840
  • 22
  • 240
  • 204
2

What I do is

 synopsis.setBackgroundColor(0);

Hope it helps!

user1256477
  • 10,763
  • 7
  • 38
  • 62
  • Maybe you should write the full code (including html) because I'm afraid the mistake is there. – user1256477 Jun 08 '12 at 06:19
  • This is the code { String textTitleStyling = ""; String titleWithStyle = textTitleStyling + "

    " + movie.synopsis + "

    "; synopsis.loadData(textTitleStyling + movie.synopsis, "text/html", "utf-8"); synopsis = (WebView) findViewById(R.id.synopsis); synopsis.getSettings(); synopsis.setBackgroundColor(0);}
    – Vervatovskis Jun 08 '12 at 08:49
2

Did you load the css in ur webview?

Something like:

synopsis.loadData(textTileStyling, "text/html", "UTF-8");

or

synopsis.loadDataWithBaseURL("", textTileStyling, "text/html", "UTF-8", "");
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Timothy
  • 21
  • 1
1

You can find a few tips here: http://code.google.com/p/android/issues/detail?id=14749 and also here: Android WebView style background-color:transparent ignored on android 2.2

Community
  • 1
  • 1
Meir Gerenstadt
  • 3,573
  • 1
  • 23
  • 22
1

Your html code sets everything to white

Replace:


    String textTitleStyling = "<head><style>* {margin:0;padding:0;font-size:20; " + 
    "text-align:justify; color:#FFFFFF;}</style></head>"; 

    String titleWithStyle = textTitleStyling + "<body><h1>" + movie.synopsis +
    "</h1></body>";

    synopsis.loadData(textTitleStyling + movie.synopsis, "text/html", "utf-8"); 
    synopsis = (WebView) findViewById(R.id.synopsis); 
    synopsis.getSettings(); 
    synopsis.setBackgroundColor(0);

With:

This excludes color from header style and applies the rest of the style only to body element


    String textTitleStyling = "<head><style>body{margin:0;padding:0;font-size:20; " + 
    "text-align:justify;}</style></head>"; 

    String titleWithStyle = textTitleStyling + "<body><h1>" + movie.synopsis +
    "</h1></body>";

    synopsis.loadData(titleWithStyle, "text/html", "utf-8"); 
    synopsis = (WebView) findViewById(R.id.synopsis); 
    synopsis.getSettings(); 
    synopsis.setBackgroundColor(0);

EDIT: fixed html

SGal
  • 1,072
  • 12
  • 13
0

You can also do it -

webview.setBackgroundColor(getContext().getResources().getColor(android.R.color.transparent));

Here android.R.color.transparent is transparent color which is belongs to android fragmework.

Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42