Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Calling JavaScript functions in WebView
    text
    copied!<p>I am trying to call some <code>javascript</code> functions sitting in an <code>html</code> page running inside an <code>android webview</code>. Pretty simple what the code tries to do below - from the android app, call a <code>javascript</code> function with a test message, which inturn calls a java function back in the android app that displays test message via toast.</p> <p>The <code>javascript</code> function looks like:</p> <pre><code>function testEcho(message){ window.JSInterface.doEchoTest(message); } </code></pre> <p>From the WebView, I have tried calling the <code>javascript</code> the following ways with no luck:</p> <pre><code>myWebView.loadUrl("javascript:testEcho(Hello World!)"); mWebView.loadUrl("javascript:(function () { " + "testEcho(Hello World!);" + "})()"); </code></pre> <p>I did enable <code>javascript</code> on the <code>WebView</code></p> <pre><code>myWebView.getSettings().setJavaScriptEnabled(true); // register class containing methods to be exposed to JavaScript myWebView.addJavascriptInterface(myJSInterface, "JSInterface"); </code></pre> <p>And heres the <code>Java</code> Class</p> <pre><code>public class JSInterface{ private WebView mAppView; public JSInterface (WebView appView) { this.mAppView = appView; } public void doEchoTest(String echo){ Toast toast = Toast.makeText(mAppView.getContext(), echo, Toast.LENGTH_SHORT); toast.show(); } } </code></pre> <p>I've spent a lot of time googling around to see what I may be doing wrong. All examples I have found use this approach. Does anyone see something wrong here?</p> <p><strong>Edit:</strong> There are several other external <code>javascript</code> files being referenced &amp; used in the <code>html</code>, could they be the issue?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload