Rick Rick - 3 years ago 192
Android Question

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download