Getting the height of WebView content after loading Android

I'm trying to apply the extension expansion function in Webview based on its content height, but I always get the wrong value. Here is my code

public class MesuredHeightWebView extends WebView { public MesuredHeightWebView(Context context) { super(context); } public MesuredHeightWebView(Context context, AttributeSet attrs) { super(context, attrs); } public MesuredHeightWebView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void invalidate() { super.invalidate(); if (getContentHeight() > 0) { // WebView has displayed some content and is scrollable. if(listener!=null) listener.updateContentHeight(getContentHeight()); } } WebViewContentHeight listener; public void setChangeContentListener(WebViewContentHeight listener) { this.listener = listener; } } 

and then in the fragment I tried to get the height of the content

  webView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); if(newProgress==100) { new Handler().postDelayed(new Runnable() { @Override public void run() { webView.setChangeContentListener(new WebViewContentHeight() { @Override public void updateContentHeight(int height) { if(height>0 && getActivity()!=null) { System.out.println("the height2 is" + height + " " + Utils.convertPixelsToDp(height, getActivity())); final int text_height = height; 

but my problem is that I always get the wrong result Thanks

+3
android html webview
source share
1 answer

Use the following methods instead of the getContentHeight() method:

 computeHorizontalScrollRange(); -> for width computeVerticalScrollRange(); -> for height 

these two methods will return the full width / height of the scroll, not the actual width / height of the web screen on the screen.

+13
source share

All Articles