Android WebView background color

AndroidWebviewBackground Color

Android Problem Overview


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

Android Solutions


Solution 1 - Android

Try using synopsis.getSettings();

WebView synopsis;
synopsis=(WebView)findViewById(R.id.synopsis);
synopsis.setBackgroundColor(Color.TRANSPARENT);

Solution 2 - Android

try below code hope use full for you:-

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

grey code : #919191

Solution 3 - Android

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

Solution 4 - Android

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"
   />

Solution 5 - Android

What I do is

 synopsis.setBackgroundColor(0);

Hope it helps!

Solution 6 - Android

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", "");

Solution 7 - Android

Solution 8 - Android

Your html code sets everything to white

Replace:

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

String titleWithStyle = textTitleStyling + "&#60body&#62&#60h1&#62" + movie.synopsis +
"&#60/h1&#62&#60/body&#62";

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 = "&#60head&#62&#60style&#62body{margin:0;padding:0;font-size:20; " + 
"text-align:justify;}&#60/style&#62&#60/head&#62"; 

String titleWithStyle = textTitleStyling + "&#60body&#62&#60h1&#62" + movie.synopsis +
"&#60/h1&#62&#60/body&#62";

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

EDIT: fixed html

Solution 9 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVervatovskisView Question on Stackoverflow
Solution 1 - AndroidRookieView Answer on Stackoverflow
Solution 2 - AndroiddugguView Answer on Stackoverflow
Solution 3 - AndroidOubaida AlQuraanView Answer on Stackoverflow
Solution 4 - Androidlive-loveView Answer on Stackoverflow
Solution 5 - Androiduser1256477View Answer on Stackoverflow
Solution 6 - AndroidTimothyView Answer on Stackoverflow
Solution 7 - AndroidMeir GerenstadtView Answer on Stackoverflow
Solution 8 - AndroidSGalView Answer on Stackoverflow
Solution 9 - AndroidGk Mohammad EmonView Answer on Stackoverflow