I followed an instruction to trigger the JavascriptInterface from a webview, but it isnt triggered in my case.
I have an class QuickTextViewer with following:
public class QuickTextViewer {
private WebView webView;
...
...
public QuickTextViewer(){
webView = dialog.findViewById(R.id.mywebview);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
super.onPageFinished(view, url);
}
}
@JavascriptInterface
public void resize(final float height) {
System.out.print(height);
}
I also added the following to proguard-rules.pro (actually public only for testing)
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}
In my case onPageFinished is triggered but resize() not ! Any suggestions/help ?
Launch an url in an external browser app from your app. startActivity(new Intent(Intent. ACTION_VIEW, Uri.
JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.
WebView allows you to bind JavaScript code to Android code through an interface. To do this, we must use the addJavaScriptInterface() method, which is passed the class that provides the interface for JS, and the name that will be used to display the instance in JS (for example, “AndroidFunction“).
Modify src/MainActivity. java file to add WebView code. Run the application and choose a running android device and install the application on it and verify the results. Following is the content of the modified main activity file src/MainActivity.
Found the problem now. Just had to add:
webView.getSettings().setJavaScriptEnabled(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With