Skip to content

Instantly share code, notes, and snippets.

@markcerqueira
Last active November 28, 2018 21:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markcerqueira/10b7ec3e526ec123429ac887a955b6b6 to your computer and use it in GitHub Desktop.
Save markcerqueira/10b7ec3e526ec123429ac887a955b6b6 to your computer and use it in GitHub Desktop.
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