Webview Javascript functions?

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:

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?
 
Joined
Mar 31, 2023
Messages
95
Reaction score
8
It looks like you're on the right track, but there are a few issues with the code you provided.

Firstly, when calling the testEcho function in your loadUrl calls, you need to pass the message as a string by enclosing it in quotes, like this:

scssCopy code
myWebView.loadUrl("javascript:testEcho('Hello World!')");
mWebView.loadUrl("javascript:(function () { " + "testEcho('Hello World!');" + "})()");

Secondly, you need to make sure that the JSInterface class is instantiated and passed to the WebView before you try to call the testEcho function. Here's an example of how you could do this:

scssCopy code
// create an instance of the JSInterface class
JSInterface myJSInterface = new JSInterface(myWebView);

// add the JSInterface instance to the WebView
myWebView.addJavascriptInterface(myJSInterface, "JSInterface");

// call the testEcho function
myWebView.loadUrl("javascript:testEcho('Hello World!')");

Finally, you need to make sure that the WebView is fully loaded before you try to call any JavaScript functions. You can do this by setting a WebViewClient and waiting for the onPageFinished callback before calling loadUrl. Here's an example:

typescriptCopy code
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// the WebView is fully loaded, so we can call testEcho now
myWebView.loadUrl("javascript:testEcho('Hello World!')");
}
});

// load the HTML file into the WebView
myWebView.loadUrl("file:///android_asset/my_file.html");

Make sure that you replace my_file.html with the name of your HTML file. Also, make sure that your HTML file contains the testEcho function.

I hope this helps! Let me know if you have any other questions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top