7

I am implementing an WebView such that it can resize its height according to the content height. I tried to do the following:

WebView view = new WebView(context);
view.loadData(htmlString, "text/html", "utf-8");

view.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        Log.d("2", view.getContentheight() + "");
        // Set the height of the webview to view.getContentHeight() here?
    }
});

Log.d("1", view.getContentHeight() + "");

Where htmlString is a String of HTML format.

However both the two Logs return 0.

I am not sure if I am doing the right thing. How can I know the content height then set the height of the WebView accordingly?

darklord
  • 5,077
  • 12
  • 40
  • 65

4 Answers4

13

this is because the onPageFinished callback means the WebView has finished reading the bytes from the network, just that. At the point onPageFinished is fired the page may not be even parsed yet.

If your webview's height is set to wrap_contents then you could do something like this:

WebView v = new WebView() {
    @Override
    public void onSizeChanged(int w, int h, int ow, int oh) {
        super.onSizeChanged(w, h, ow, oh); // don't forget this or things will break!
        Log.d(TAG, "WebView height" + getContentHeight());
    }
};

If your webview is not wrap_contents then one option is to do what josedlujan suggested in his answer to this question and fetch the height from JavaScript.

Another option would be to use the deprecated PictureListener:

webview.setPictureListener(new PictureListener() {
    int previousHeight;
    @Override
    public void onNewPicture(WebView w, Picture p) {
        int height = w.getContentHeight();
        if (previousHeight == height) return;
        previousHeight = height;
        Log.d(TAG, "WebView height" + height); 
    }
});
Community
  • 1
  • 1
marcin.kosiba
  • 3,221
  • 14
  • 19
3

you can obtain the height and width with javascript from the webview.

WebView view = new WebView(context);
view.loadData(htmlString, "text/html", "utf-8");
view.getSettings().setJavaScriptEnabled(true);
view.setSaveEnabled(true);
view.addJavascriptInterface(new JavaScriptInterface(), "HTMLOUT");
view.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url){
  view.loadUrl("javascript:window.HTMLOUT.getContentHeight
        (document.getElementByTagName('html')") 
    }
});
view.loadDataWithBaseURL(null,htmlString,"text/html","utf-8,null");

   }



 class JavascriptInterface{
   public void getContentHeight(String value){
       if (value != null){
          webviewContentHeight = Integer.parseInt(value);
             Log.d(LOG_TAG("result from js"+webviewContentHeight);
             Toast.makeText(activity,"Content Height is:"+webviewContentHeight,
                     Toast.LENGTH_SHORT).show();
           }
       }
   }
josedlujan
  • 5,357
  • 2
  • 27
  • 49
2

Here is working code to get webview scrollheight on view finished loading:

WebView webviewPageTest=new WebView(this);
webviewPageTest.getSettings().setJavaScriptEnabled(true);
class JsObject {
    @JavascriptInterface
       public void toString(String jsResult) {
           Log.v("scrolH:",jsResult);
           //use this if you need actual onscreen measurements 
           //float webViewHeight = (Integer.parseInt(jsResult) * getResources().getDisplayMetrics().density);
       }
    }

webviewPageTest.addJavascriptInterface(new JsObject(), "HTMLOUT");
webviewPageTest.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:( function () { var h = document.body.scrollHeight; window.HTMLOUT.toString(h); } ) ()");
        }
    });

this.addContentView(webviewPageTest, params);

worked of here Łukasz Sromek and josedlujan above and here android dev docs

Boris Gafurov
  • 1,427
  • 16
  • 28
-3

You can do this by providing some delay in onPageFinished()

webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    int contentHeight = webView.getContentHeight();
                    int viewHeight = webView.getHeight();
                    Toast.makeText(context, "*** " + contentHeight + " - " + viewHeight, Toast.LENGTH_SHORT).show();
                }
            }, 500);
        }
    });