Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to javascript calls of a WebView

I has the following HTML

<div>
    <input type="submit" onClick="anyJSFunction('Test')">
</div>

And I open it on a WebView like:

        web = new WebView(this);

        WebSettings settings = web.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setSupportMultipleWindows(false);
        settings.setSupportZoom(false);
        settings.setPluginsEnabled(true);
        settings.setDomStorageEnabled(true);

        web.setVerticalScrollBarEnabled(false);
        web.setHorizontalScrollBarEnabled(false);

        web.loadUrl("file:///sdcard/test.html");

Now I want to listen on Java when 'anyJSFunction' is called and also be able to get it arguments.

How can it be done?

like image 848
Marcos Vasconcelos Avatar asked Jan 18 '26 09:01

Marcos Vasconcelos


1 Answers

There's a decent example in the documentation for WebView. Look for "Binding JavaScript code to Android code."

You'll build a class as an interface from JavaScript to Java/Android. You create an instance and pass it to the WebView. Then the JavaScript code can reference the class instance by name.

like image 136
WiredPrairie Avatar answered Jan 21 '26 01:01

WiredPrairie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!