14

I have a RelativeLayout with WebView. I am loading some generated html text into that WebView. After loading the data WebView height exceeding the screen. Now I need the height of the entire WebView. Till now I tried WebView#getHeight(), WebView#getBottom() but I'm getting the height of visible content.

How to get the total height of WebView.? screen height

2
  • 1
    you have to use javascript for that.
    – njzk2
    Commented Apr 13, 2017 at 14:10
  • 1
    Can you tell me how ?
    – shobhan
    Commented Apr 14, 2017 at 5:06

8 Answers 8

18

Finally I got the answer for my question. Here is the answer.

After loading WebView we will get call in WebViewClient#onPageFinished there we need to call javascript to get the height of full content of WebView

WebViewClient class

/**
 * Custom web client to perform operations on WebView
 */
class WebClient extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:AndroidFunction.resize(document.body.scrollHeight)");
        }
    }
}

After that we will get callback with height in WebInterface class which is Registered as JavascriptInterface

/**
 * WebView interface to communicate with Javascript
 */
public class WebAppInterface {

    @JavascriptInterface
    public void resize(final float height) {
        float webViewHeight = (height * getResources().getDisplayMetrics().density);
        //webViewHeight is the actual height of the WebView in pixels as per device screen density
    }
}
8
  • resizing is crucial if actual height in screen pixels is needed. thanks a lot.
    – BGS
    Commented Jul 6, 2017 at 4:14
  • hey, what is readWebView? Ah- an instance of WebAppInterface?
    – pale bone
    Commented Sep 8, 2018 at 2:24
  • No it's webView instance. Edited answer. Thanks man. I forgot that it's my answer. ;)
    – shobhan
    Commented Sep 10, 2018 at 13:10
  • how to use this class WebAppInterface
    – Nanda Z
    Commented Mar 11, 2019 at 9:49
  • 1
    Since WebViewClient.onPageFinished() only gets called when the page had finished loading, this solution falls short when the size of the content changes post loading (e.g. Javascript content rendered after page finished loading, user interactions with expandable and collapsible sections)
    – trod
    Commented Dec 2, 2021 at 5:43
5

Wrap your WebView within a ScrollView and webView.getHeight() will return the height of the actual WebView content. Make sure your WebView's height attribute is set to wrap_content.

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </WebView>
</ScrollView>
5
  • I tried this case also, but if we load 10 high quality images it is giving out off memory error. And one more thing keeping WebView in ScrollView is not recommended.
    – shobhan
    Commented Apr 28, 2017 at 5:39
  • @shobhan You're probably getting OOM because the images are much higher res than what the device can handle, consider if a high res image is 50mb, you load 10 = 250mb, and then on top of that you have the app, the os, etc. running on oftentimes devices with less than a gig of ram, you're going to kill your app quickly. Commented Jul 20, 2017 at 5:01
  • I think you tried to explain part of my above comment. (OOM out off memory error) :P Thanks.
    – shobhan
    Commented Jul 20, 2017 at 6:53
  • If you are using scroll view among with nested scroll view try to set android:nestedScrollingEnabled="true" inside scroll view
    – bene25
    Commented Jan 17, 2023 at 9:37
  • NEVER wrap webViews inside scrollviews. It will make the WebView expand to max size and the webView will believe all of it's content is shown. WebView should handle content visibility, not ScrollView. Commented May 22, 2023 at 8:21
3

kotlin: myWebView.contentHeight

2
  • 2
    In my case I aloso need multiply myWebView.contentHeigth to Resources.getSystem().displayMetrics.density to get real height. Commented Aug 26, 2022 at 5:05
  • Last comment was what I needed. I will post this as new answer just in case anyone misses it.
    – Androidz
    Commented Jun 3, 2024 at 8:57
2
public class CustomWebView extends WebView{
    public CustomWebView(Context context) {
        super(context);
    }
    int ContentHeight = 0;
    public int getContentHeight(){
        if(ContentHeight==0)
            ContentHeight = computeVerticalScrollRange();
        return ContentHeight;
    }
}

Webview has protected method computeVerticalScrollRange(). you can access this method by creating custom one.

((CustomWebView) webView).getContentHeight())
3
  • the word compute tends to indicate that some work is being done, possibly heavy.
    – njzk2
    Commented Apr 13, 2017 at 14:25
  • I get a ClassCastException with this solution. Any work arounds?
    – Ivan
    Commented Apr 26, 2017 at 16:50
  • Nevermind, I figured it out. See my full answer below.
    – Ivan
    Commented Apr 26, 2017 at 17:45
-1

In your Java class add the getWindow()

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_webview);
}
-1

My Answer:-

I think you have to remove padding properties from your layout.xml file

(i.e) Remove the following content from your XML file and try it.

android:paddingBottom    
android:paddingLeft    
android:paddingRight    
android:paddingTop

Hope it helps!!

-1
private int getScale(){
    Display display = ((WindowManager) 
    getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    int width = display.getWidth(); 
    Double val = new Double(width)/new Double(PIC_WIDTH);
    val = val * 100d;
    return val.intValue();
}

check this link! for more information

-1

Actual height in Kotlin: myWebView.contentHeight * Resources.getSystem().displayMetrics.density

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.