5

I faced the same issue here. Basically I have text-webview-text in vertical linear layout as shown below:

 __________
| Textview |
|__________|
|  WebView | <-can zoom
|__________|
| TextView |
|__________|

The webview has a zoom toggle, when the content zooms larger webview will adjust to a bigger size and push down the textview:

 __________
| Textview |
|__________|
|          |
|          |
|  WebView |
|          |
|__________|
| TextView |
|__________|

Until now everything is fine. The problem is, when I zoom back to smaller size, the content inside webview shrinks but webview itslelf is still same size, leaving a large gaping empty space:

 __________
| Textview |
|__________|
|  WebView |
|          |
|          |
|          |
|__________|
| TextView |
|__________|

My problem is also similar to this one. Calling invalidate() won't work, and I don't want to recreate the fragment without destroying the activity (I actually don't know how). My question is, how can I update/resize the height of the webview when I zoom it from larger to smaller? (NOTE: I add all the views dynamically, so please show me how I can solve it programatically. Thanks)

This weird problem doesn't exist in Froyo 2.2 but it is a bugging issue in JB 4.2.2 real device.

Community
  • 1
  • 1
Neoh
  • 15,906
  • 14
  • 66
  • 78
  • The answer to this question seems to be what you are looking for. It is exactly what I was looking for. http://stackoverflow.com/questions/15546416/how-to-shrink-webview-size-dynamically-according-to-its-content – jnrcorp Mar 30 '14 at 16:32

1 Answers1

0

Add the code snippet as follow, it will set the height of webview when the page is loaded

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) {
    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            ViewGroup.LayoutParams lp = webView.getLayoutParams();
            lp.width = getResources().getDisplayMetrics().widthPixels;
            lp.height = (int) (height * getResources().getDisplayMetrics().density);
            webView.setLayoutParams(lp);
        }
    });
}

Modified from solution on https://capdroid.wordpress.com/2014/08/07/resizing-webview-to-match-the-content-size/

Chiu
  • 374
  • 2
  • 10