Setting webview background image to a resource graphic in Android

JamieBennett picture JamieBennett · Aug 11, 2009 · Viewed 29k times · Source

I'm trying to set the background of a webview to a drawable image resource in Android.

From the sdk it seems something like this would work but it doesn't.

WebView web = (WebView) findViewById(R.id.webView);

web.setBackgroundResource(R.drawable.backgroundmain);
web.loadData(profile, "text/html", "UTF-8");

Any idea's?

Answer

JamieBennett picture JamieBennett · Aug 11, 2009

As always you tend to figure these things out as soon as you ask the question. For the benefit of others, the layout that I'm using, LinearLayout, can take a background parameter that can be a colour or a resource.

So in my views .xml file I simply added a

android:background="@+drawable/backgroundmain"

and use

web.setBackgroundColor(0);

To make the webview transparent to see the background image behind.