WebView height = wrap_content with font resizing not working

I have a webview with layout_height = "wrap_content". If you increase the default font size, the height of the web view increases. But if I reduce the default font size, then the height of the webview will not decrease. Thus, there was an empty spot at the bottom.

I tried the following trick:

articleContent.getSettings().setDefaultFontSize(fontSize); RelativeLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.BELOW, subtitleTextView.getId()); articleContent.setLayoutParams(layoutParams); 

But it did not help. I know there is a way to fix this by re-creating the WebView with the code when I change the default font size, but in my situation I cannot do this. I need to save the webview because there are some views, and I cannot recreate them either.

+6
source share
7 answers

try it

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <WebView android:id="@+id/webview1" android:layout_width="wrap_content" android:layout_height="fill_parent" /> </LinearLayout> 
-6
source

The only solution I found that really works is in the com.android.email client that comes with the Android Open Source Project. There they have a RigidWebView that works with wrap_content and resizes accordingly when the content changes. The source code for this can be found here:

https://android.googlesource.com/platform/packages/apps/Email/+/c19c1226da4a32e49e81c202478384f0348bcfef/src/com/android/email/view/RigidWebView.java

You will need the Clock and Throttle classes, but with minor changes to the code, you can make it work in your application. Then wherever you usually define a WebView, you can use RigidWebView instead.

+3
source

The issue is with Android issue tracking:
https://code.google.com/p/android/issues/detail?id=18726

If you really want WebView to wrap your content, it seems that the only job at the moment is to create a new WebView every time your content changes (which may not be acceptable when targeting low memory devices). This is a decision I made anyway.

+2
source

This issue should be fixed in KitKat WebView.

For WebView versions, there are no good workarounds in front of KitKat that I know of. Changing the width of the WebView should result in a recalculation of the height, but it can also lead to failures.

I think that by temporarily pressing height 0, and then throwing it back to wrap_contents, you also need to get rid of excess filling.

+1
source

Try it,

 webview.setVisibility(View.GONE); webview.setVisibility(View.VISIBLE); 
+1
source

Just add this

 LinearLayout.LayoutParams wv_l = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); wv.setLayoutParams(wv_l); 
+1
source

Try the following combination:

 <LinearLayout android:id="@+id/xxxxxxxxxxxxxxxxxxx" android:layout_width="match_parent" android:layout_height="200dp" android:orientation="vertical" > <TextView android:id="@+id/yyyyyyyyyyyyyyyyyyyyy" style="@style/Etiquetas" android:text="@string/hola" /> <WebView android:paddingLeft="10dp" android:id="@+id/zzzzzzzzzzzzzzz" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" > </WebView> </LinearLayout> 

Java:

 LinearLayout. LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); xxxxxxxxxxxxxxxxxxx.setLayoutParams(params); WebView zzzzzzzzzzzzzzz = (WebView) findViewById(R.id.zzzzzzzzzzzzzzz); zzzzzzzzzzzzzzz.setBackgroundColor(0x00000000); zzzzzzzzzzzzzzz.loadData(Html.fromHtml(mistringsqlite).toString(), "text/html", null); 
-3
source

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


All Articles