- Joined
- Jun 29, 2022
- Messages
- 28
- Reaction score
- 0
I'm attempting to invoke certain javascript functions from an html page running inside an android webview. The code below attempts to perform something very simple: from the android app, call a javascript function with a test message, which in turn runs a java function back in the android app that shows the test message via toast.
The javascript function is as follows:
I tried calling the javascript from the WebView in the following methods without success:
I had JavaScript enabled in the WebView.
And now for the Java Class.
I've spent a lot of time googling to figure out what I'm doing wrong. I utilised this strategy in all of the examples I found here. Is there something wrong here?
The javascript function is as follows:
JavaScript:
function testEcho(message){
window.JSInterface.doEchoTest(message);
}
I tried calling the javascript from the WebView in the following methods without success:
JavaScript:
myWebView.loadUrl("javascript:testEcho(Hello World!)");
mWebView.loadUrl("javascript:(function () { " + "testEcho(Hello World!);" + "})()");
I had JavaScript enabled in the WebView.
JavaScript:
myWebView.getSettings().setJavaScriptEnabled(true);
// register class containing methods to be exposed to JavaScript
myWebView.addJavascriptInterface(myJSInterface, "JSInterface");
And now for the Java Class.
JavaScript:
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();
}
}
I've spent a lot of time googling to figure out what I'm doing wrong. I utilised this strategy in all of the examples I found here. Is there something wrong here?