Android WebView adds weird blank space at the bottom

I am trying to add more WebView space to a loaded WebView using JavaScript .

I am loading data using loadDataWithBaseUrl() from a String resource. Then, after measuring some content, I need to make the WebView larger, say, by 100 pixels. Therefore, I call the javascript function using loadDataWithBaseUrl() :

 mWebView.loadUrl("javascript:addHeight()"); 

JavaScript function defined in the source HTML:

 function addHeight() { " + var elemDiv = document.createElement('div'); elemDiv.style.cssText = 'height:100px;'; document.body.appendChild(elemDiv); } 

After that, the WebView resized to fit the new content. However, the resulting height is not the original Size + 100px, but more. I do not know where the problem is. I cannot determine the size of the target, because it depends on the size of the added div , and it will always be different.

Has anyone tried this? Are you familiar with this behavior?

+6
source share
1 answer

I found the following solution . There used a JS callback. I hope this helps you. This resizing should work better than dynamic.

  private void setupWebView() { webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { webView.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)"); super.onPageFinished(view, url); } }); webView.addJavascriptInterface(this, "MyApp"); } @JavascriptInterface public void resize(final float height) { MyActivity.this.runOnUiThread(new Runnable() { @Override public void run() { webView.setLayoutParams(new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density))); } }); } 

And see paragraph 4 of this list . Good luck

+2
source

All Articles