Android: text often disappears in webview when LAYER_TYPE_SOFTWARE is set

I am trying to add a webview to a scrollview that can display static HTML text with a 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 do not set the value of setLayerType, I can see the text correctly. However, a flickering effect is added when scrolling. If the setLayerType line is added, the text for some pages disappears. This is what 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 the .apk file on my mobile and not on the emulator. I am using Android version 4.1.2. Can someone tell me what needs to be done so that the text appears without a flickering effect and with a transparent background?

+4
source share
2 answers

add #android: layerType = "software" # to the WebView and the entire container up to 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> 
+2
source

I am facing the same problem and I solved it; webview in scrollview software and webview setlayertype, so you need setlayertype software for scrollview; as below:

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

Hope this helps you.

+1
source

Source: https://habr.com/ru/post/1498997/


All Articles