Forked from markcerqueira/WebViewMeasuringActivity.java
Created
April 24, 2018 03:32
-
-
Save flyfire/bd32e20f46aa84ffbc9b85806b147c36 to your computer and use it in GitHub Desktop.
Android WebView Content Measuring
This file contains hidden or 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