Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the full height of in android WebView?

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

like image 669
shobhan Avatar asked Apr 13 '17 14:04

shobhan


2 Answers

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
    }
}
like image 65
shobhan Avatar answered Sep 20 '22 16:09

shobhan


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>
like image 22
Ivan Avatar answered Sep 20 '22 16:09

Ivan