6

I am developing an android game application,I have implemented all the screens.Now i want to change the webview background color,Can anybody guide me.Here is my xml file

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background"> <WebView android:id="@+id/webbrowser" android:layout_width="fill_parent" android:layout_height="345px" android:layout_marginTop="46px"/> <Button android:id="@+id/Btn" android:background="@drawable/back_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="109px" android:layout_marginTop="37px"> </Button> </LinearLayout> 

And My Java file is package com.tli.roadtripbingo;

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; public class WebView1 extends Activity { private Button Back; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.webview); Back = (Button)findViewById(R.id.back); WebView webView = (WebView) findViewById(R.id.webbrowser); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("http://www.vikingredning.no/skilt.aspx"); webView.setWebViewClient(new HelloWebViewClient()); } class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }; } 

Thanks in advance Regards Tushar

4 Answers 4

29

You can find answer here Change Background color and font color

WebView myWebView = (WebView) findViewById(R.id.myWebView); myWebView.setBackgroundColor(Color.parseColor("#123456")); 
Sign up to request clarification or add additional context in comments.

2 Comments

It's strange, but myWebView.setBackgroundResource(android.R.color.black); doesn't work for me (API level 16).
@3k: That's because android.R.color.black isn't a resource. From the documentation: "Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background."
5

You can make the WebView transparent like this:

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

1 Comment

You are always using code snippets for normal code. Code Snippets are only supposed to be used for HTML or javascript or other code which can be run in the browser. You cannot run Java in the browser. Use normal code blocks in the future... I will edit your answer for you this time and fix the formatting etc, but please don't do this anymore in the future. This isn't the first time I told you about this...
4
 <LinearLayout android:id="@+id/web_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/web_bg_color" android:gravity="center" > <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> WebView mWebView = (WebView) findViewById(R.id.webview); mWebView.setBackgroundColor(Color.TRANSPARENT); mWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); 

It worked for me in lollipop also.

try it once!

Comments

-2

You may also want to reload the page after changing colors by using the reload() method:

webView.reload(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.