Rick
Rick

Reputation: 1163

Semi transparent webview background in android

EDIT: My Current solution. Was wrap the json in a div tag and set it to transparent as well.

I am populating a webview from a json object remotely.

{
"message": "<p style=\"color:#EEEEEE;background-color:transparent;font-family:'Arial Narrow','Nimbus Sans L',sans-serif;\"><\/p><p style=\"color:#EEEEEE;background-color:transparent;font-family:'Arial Narrow','Nimbus Sans L',sans-serif;\">The mission of the Canton Police Department is to protect the lives and properties of the citizens of Canton, enforce all city, state, and federal law<\/p>",
"nationalad": ""
}

I can get the webview background to be transparent. But not semi transparent. When I try this:

webView1.setBackgroundColor(Color.argb(128, 00, 00, 000));

I get a black background with a tiny vertical transparent strip on the right. To make it transparent all the way:

webView1.setBackgroundColor(0x00000000);  

Here is how I am bringing in the JSON:

  try{
                JSONObject json = new JSONObject(result);
                JSONArray nameArray = json.names();
                JSONArray valArray = json.toJSONArray(nameArray);
                for (int i = 0; i < valArray.length(); i++)
                {      
                    WebView webView1 = (WebView) findViewById(R.id.webView1);
                   // webView1.setBackgroundColor(0x00000000);

                    //webView1.loadDataWithBaseURL(null, valArray.getString(0), "text/html", "utf-8", null);   
                    webView1.setBackgroundColor(Color.argb(128, 00, 00, 00));
                }     

EDIT: XML

    <!-- Included header.xml here -->

    <ViewStub
        android:id="@+id/vsHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inflatedId="@+id/header"
        android:layout="@layout/header" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/cpd"
        android:gravity="center|center_horizontal|center_vertical"
        android:orientation="vertical"
        android:paddingBottom="50dp" >

        <WebView
            android:id="@+id/webView1"
            android:layout_width="250dp"
            android:layout_height="300dp"
            />

    </LinearLayout>

</LinearLayout>         

enter image description here

Upvotes: 0

Views: 3442

Answers (3)

mhk
mhk

Reputation: 112

just try this :

webView.setAlpha(0);  /*or for semi transparent : webView.setAlpha(0.5f);*/

Upvotes: 0

Meir Gerenstadt
Meir Gerenstadt

Reputation: 3593

This works for me:

on your web page body:

background-color:transparent

on your java activity:

webView.loadUrl("yourwebpage");
webView.setBackgroundColor(0x00000000); 
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);

and you must set the hardwareAccelerated false for your activity:

android:hardwareAccelerated="false"

Upvotes: 4

Bobbake4
Bobbake4

Reputation: 24847

Change your setBackgroundColor call to the following:

    webView1.setBackgroundColor(0xAA000000);  

This should give you a semi transparent black. The first two entries are the alpha values. The lower the value the more transparent the color will be.

Upvotes: 2

Related Questions