Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Text disappears frequently in webview when LAYER_TYPE_SOFTWARE is set

I am trying to add a webview within scrollview which can display static HTML text with transparent background. Here is the code

        WebView descr = (WebView) view.findViewById(R.id.description);
        descr.setBackgroundColor(Color.TRANSPARENT);
        descr.loadData(desc, "text/html", "utf-8");
        descr.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

If I don't set setLayerType value, I can see the text correctly. However it adds flickering effect when the scrolling is done. If setLayerType line is added, text for some of the pages disappears. Here is how my layout looks like

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/title" >

    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:descendantFocusability="blocksDescendants">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:contentDescription="@id/title"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            />

        <WebView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:textAlignment="gravity"
            android:textSize="14sp"
            android:background="@null"
            android:focusable="false" />
    </LinearLayout>
</ScrollView>

This problem occurs when i install .apk file on my mobile and not on emulator. I am using android version 4.1.2. Can someone let me know what need to be done so that the text would be displayed without flickering effect and with transparent background?

like image 785
Rajan Phatak Avatar asked Aug 26 '13 16:08

Rajan Phatak


2 Answers

please add #android:layerType="software"# to the WebView and all the container until ScrollView. on Android4.4, it works for me.

<ScrollView
    android:layerType="software"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/title" >

    <LinearLayout 
        android:layerType="software"
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:descendantFocusability="blocksDescendants">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:contentDescription="@id/title"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            />

        <WebView
            android:layerType="software"
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:textAlignment="gravity"
            android:textSize="14sp"
            android:background="@null"
            android:focusable="false" />
    </LinearLayout>
</ScrollView>
like image 122
zxshi Avatar answered Nov 15 '22 05:11

zxshi


I encounter the same problem, and I resolved it; webview in scrollview and webview setlayertype software, so you need setlayertype software for scrollview; like below:

<ScrollView
    android:layout_width="match_parent"
    android:layerType="software"
    android:layout_height="match_parent"
    android:layout_below="@+id/title" >

I hope this can help you.

like image 32
Mejonzhan Avatar answered Nov 15 '22 05:11

Mejonzhan