Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView height = wrap_content with changing font size doesn't work

I have webview with layout_height = "wrap_content". If I increase default font size, then webview height increases too. But if I decrease default font size, then webview height doesn't decrease. So there is empty space on the bottom remaining.

I've tried 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 didn't help. I know there is the way to fix it by recreating WebView with a code when I changing default font size, but in my situation I can not do like that. I need to keep webview, because there are some views bellow it, and I can not recreate them too.

like image 727
Dmitriy Avatar asked Mar 19 '12 04:03

Dmitriy


4 Answers

The only solution I have found that actually works, is in the com.android.email client that comes with the Android Open Source Project. There, they have a RigidWebView which works with the wrap_content and will resize accordingly when the content changes. 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'll need the Clock and Throttle classes that come with it, but with minor changes to the code, you can make it so it works in your app. Then anywhere where you would normally define a WebView, you can use RigidWebView instead.

like image 143
kingargyle Avatar answered Nov 07 '22 14:11

kingargyle


The issue is referenced Android's issue tracker here:
https://code.google.com/p/android/issues/detail?id=18726

If what you really want is a WebView wrapping your content it looks like the only work-around at the moment is to create a new WebView every time your content changes (which may not be acceptable when targeting low memory devices). It is the solution I have adopted anyway.

like image 37
Thibault D. Avatar answered Nov 07 '22 12:11

Thibault D.


Try this,

webview.setVisibility(View.GONE);
webview.setVisibility(View.VISIBLE);
like image 2
Basim Sherif Avatar answered Nov 07 '22 14:11

Basim Sherif


This issue should be fixed in the KitKat WebView.

For WebView versions before KitKat there aren't any good workarounds I know of. Altering the width of the WebView should force a height recalculation but it might also lead to visual glitches.

I think that temporarily forcing a height of 0 and then flipping it back to wrap_contents should get rid of the excess padding too.

like image 1
marcin.kosiba Avatar answered Nov 07 '22 13:11

marcin.kosiba