Mustansir
Mustansir

Reputation: 2515

How to change background in webview

I have created two themes for my activity; Bright/Dark. But when i trigger change in theme, color of webpage loaded in the webview doesn't change.

I have tried this myWebView.setBackgroundColor(Color.TRANSPARENT); but it doesn't make any difference. So, how would i go about this?

Here is my layout please have a look;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">   

    <LinearLayout
        xmlns:night="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@color/ColorPrimary"
        night:night_background="@color/colorPrimary_n"
        tools:ignore="MissingPrefix">

        <ProgressBar
            android:id="@+id/progressBar3"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="3dp"
            android:max="100"
            android:progressDrawable="@drawable/greenprogress"
            />

        <android.support.v4.widget.SwipeRefreshLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <WebView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/webViewTop"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                night:night_background="@color/colorPrimary_n"
                android:layout_below="@id/progressBar3"/>

        </android.support.v4.widget.SwipeRefreshLayout>

    </LinearLayout>
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

Upvotes: 4

Views: 7731

Answers (4)

Rudresh
Rudresh

Reputation: 751

Put this code in xml:

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


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

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

yourWebView.setBackgroundColor(Color.TRANSPARENT);

Upvotes: -1

Mustansir
Mustansir

Reputation: 2515

Ok so after searching around, i found out that, to change the color of webpage after it has loaded, CSS customization is needed, no settings in webview can make webpage transparent(at least in my case).

Upvotes: 0

user4584887
user4584887

Reputation:

You can achieve it programmatically like below

webView.getSettings();
webView.setBackgroundColor(Color.TRANSPARENT);

I think you should go with layer type too

mWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);

GOOD LUCK:)

Upvotes: 3

Mukesh
Mukesh

Reputation: 258

You can try this in java

webView.setBackgroundColor(Color.TRANSPARENT);
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);

Upvotes: 0

Related Questions