Android WebView background color
Asked Answered
C

9

67

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);
Clamatorial answered 6/6, 2012 at 17:35 Comment(0)
E
116

Try setting the background like this:

WebView synopsis;
synopsis=(WebView)findViewById(R.id.synopsis);
synopsis.setBackgroundColor(Color.TRANSPARENT);
Ensilage answered 6/6, 2012 at 18:27 Comment(3)
this method is working only if you specify color directly like: synopsis.setBackgroundColor(Color.Black); tested on Samsung Tab 4 7" android 4.4.2Mccary
what's the point of using "getSettings()"? you are not using it.Uneducated
I agree, "geSettings()" is not necessary. The solution worked quite nice though :)Aleenaleetha
I
33

try below code hope use full for you:-

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

grey code : #919191

Inscribe answered 26/12, 2012 at 5:35 Comment(0)
L
22

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);
Lysimeter answered 19/2, 2014 at 15:40 Comment(2)
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)Jayson
If using Xamarin, webview.SetBackgroundColor (Android.Graphics.Color.Transparent); is enough.Eric
D
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"
   />
Dg answered 21/1, 2021 at 16:40 Comment(0)
A
2

What I do is

 synopsis.setBackgroundColor(0);

Hope it helps!

Agility answered 6/6, 2012 at 19:50 Comment(2)
Maybe you should write the full code (including html) because I'm afraid the mistake is there.Agility
This is the code { 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);}Clamatorial
C
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", "");
Centonze answered 10/6, 2012 at 8:42 Comment(2)
or synopsis.loadDataWithBaseURL("", textTileStyling, "text/html", "UTF-8", "");Centonze
Thanks for your answer, I edited my post, you will find how i did load dataClamatorial
H
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

Hoick answered 5/7, 2013 at 14:22 Comment(0)
F
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.

Flexor answered 14/7, 2020 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.