Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android WebView Content Measuring
public class WebViewMeasuringActivity extends RelativeLayout {
public void loadHtml(String html)
WebView webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebViewResizer(), "WebViewResizer");
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView webView, String url) {
webView.loadUrl("javascript:window.WebViewResizer.processHeight(document.querySelector('body').offsetHeight);");
super.onPageFinished(view, url);
}
});
webView.loadData(html, mimeType, encoding);
}
private class WebViewResizer {
@JavascriptInterface
public void processHeight(String height) {
// height is in DP units. Convert it to PX if you are adjusting the WebView's height.
// height could be 0 if WebView visibility is Visibility.GONE.
// If changing the WebView height, do it on the main thread!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.