4

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>
gevorg
  • 4,835
  • 4
  • 35
  • 52
Mustansir
  • 2,445
  • 1
  • 21
  • 32

4 Answers4

3

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

  • what webView.setBackgroundColor(Color.TRANSPARENT); does that, it initially makes webview transparent, and in the start of the activity i can see my theme color, but after webpage starts to load it disappears, and color of webpage take places. – Mustansir May 30 '16 at 13:06
0

You can try this in java

webView.setBackgroundColor(Color.TRANSPARENT);
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
Mukesh
  • 266
  • 1
  • 4
  • 10
0

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

Mustansir
  • 2,445
  • 1
  • 21
  • 32
-1

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);
Rudresh
  • 731
  • 1
  • 10
  • 26
  • 1
    No it doesn't work. Before loading of webpage, it remains transparent, but as soon as it loads, it acquires the colors of webpage – Mustansir May 30 '16 at 17:33
  • 1
    At least try to test it yourself first because WebView does not allowed background at all. – Mihae Kheel Mar 19 '21 at 16:08