Android 7 WebView with wrap_content

I have a WebView with android:layout_height="wrap_content" inside a ScrollView . Prior to Android 7, this led to the resizing of the WebView to the height of the local html content that I installed using loadData . On my Nexus 5X with Android 7, however, the height of the WebView seems unreliable, sometimes it only shows parts of the first text line, sometimes there is a big space at the end of the content.
I think this may be due to the fact that Google now uses Chrome for WebViews, starting with Nougat. Does anyone have an explanation or fix / workaround for this problem?

It may also be important that the views are contained in cells of a RecyclerView .

+8
android html google-chrome webview scrollview
source share
2 answers

Using WebView to display content inside RecyclerView is a bit risky and not as efficient. You can’t imagine how it works on different devices!

I suggest changing your decision using TextView and converting HTML content to formatted text using this command

 textView.setText(Html.fromHtml(<YOUR HTML CONTENT>)) 
By the way, these are some of the efforts that I made using WebView to achieve the size of the patch to display the content of the banner ad:
 public class AdsWebView extends WebView { // some setup codes... private static final int MAX_SCALE = 120; public AdsWebView(Context context) { setVerticalScrollBarEnabled(false); setHorizontalScrollBarEnabled(false); setScrollbarFadingEnabled(false); setupAdsScale(); } private int getAdsScale() { int width = getDisplayWidth(getContext()); Double val = Double.valueOf(width) / Double.valueOf(480); val = val * 100d; return val.intValue(); } private void setupAdsScale() { int scale = getAdsScale(); if (scale > MAX_SCALE) scale = MAX_SCALE; setInitialScale(scale); } private int getDisplayWidth(Context context) { try { Activity activity = (Activity) context; if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) < 13) { Display display = activity.getWindowManager() .getDefaultDisplay(); return display.getWidth(); } else { Display display = activity.getWindowManager() .getDefaultDisplay(); Point size = new Point(); display.getSize(size); return size.x; } } catch (Exception e) { e.printStackTrace(); return 0; } } 
+1
source share

Workaround:

Waiting for the html page to load and launch the internal JS page to determine the height of the content and set it in the WebView Layout settings.

Easy to run JS inside the page, just go to url like

 javascript:Math.max(document.body.scrollHeight,document.body.offsetHeight,document.documentElement.clientHeight,document.documentElement.scrollHeight,document.documentElement.offsetHeight); 

To pass the result to Java code, you must provide the Java-Java Script interface as described here https://developer.android.com/guide/webapps/webview.html (binding JavaScript code to Android code)

Your navigation url should look like

 javascript:myfunc(Math.max(document.body.scrollHeight,document.body.offsetHeight,document.documentElement.clientHeight,document.documentElement.scrollHeight,document.documentElement.offsetHeight)); 

Myfunc will be called and you will get the page height. Now just set the height to the height of the WebView.

+1
source share

All Articles