Last active
November 28, 2018 21:21
-
-
Save markcerqueira/10b7ec3e526ec123429ac887a955b6b6 to your computer and use it in GitHub Desktop.
Android WebView Content Measuring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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