'Getting click event from the webview in android

I am using webView and I want to get the click event of the button on the webview in my app and want to perform operation from my side . But I am unable to get the click event. I have tried many methods searched from google by enabling the javascript from the webview but no any method worked. Please help Thanks in advance.

I am using this code where valid is the name of the button in the HTML file.

webView.addJavascriptInterface(new Object() {

        @JavascriptInterface
        public void performClick() {

            GeneralFunctions.showToast(HomeActivity.this, "clicked");
        }
    }, "valid");


Solution 1:[1]

Here I am describing a code block of click event of webview (JavaScript) you can modify your code like this

WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new Object()
{
   @JavascriptInterface           // For API 17+
   public void performClick(String strl)
   {
      stringVariable = strl;
      Toast.makeText (YourActivity.this, stringVariable, Toast.LENGTH_SHORT).show();
   }
}, "valid");

And HTML

<button type="button" value="someValue" onclick="valid.performClick(this.value);">Valid</button>

Solution 2:[2]

So Finally I got the answer. After enabling the Javascript. I have to enable the

webView.getSettings().setDomStorageEnabled(true);

So that performClick can work.The correct code is;

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true); // This line will be added
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.loadUrl("https://www.example.com/");
    webView.setWebViewClient(new WebViewClient());

    webView.addJavascriptInterface(new Object(){
        @JavascriptInterface
        public void performClick(String val){

            GeneralFunctions.showToast(HomeActivity.this,val+"clicked");
        }
    },"ok");

Solution 3:[3]

100% working example - I had to listen a event from webview in android app. The below event was fired from Website in webview (iFrame).

  window.addEventListener('event_name_goes_here', function (response) {
        if (response.data.status === 200) {
            console.log(response.data.message);
        }
});

In Android to listen this event write below code in onPageFinished on webview

 binding.webview.loadUrl(
                        "javascript:(function() {" +
                                "window.addEventListener('event_name_goes_here', function(response) {" +
                                " Android.receiveMessage(JSON.stringify(response.data));});" +
                                "})()"

                )

Write inner class to fetch the event

class JsObject(val context: Context) {
    @JavascriptInterface
    fun receiveMessage(data: String): Boolean {
        Log.e("data_from_web", data)
        Toast.makeText(context,data.toString(),Toast.LENGTH_LONG).show()
        return true
    }

Initilize webview like this

 binding.webview.apply {          
        loadUrl(mUrl ?: "")
        settings.javaScriptEnabled = true
        settings.setSupportZoom(false)
        settings.domStorageEnabled = true
        addJavascriptInterface(JsObject(requireContext()), "Android")       
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vijay Pal Vishwakarma
Solution 2 I.d007
Solution 3 Sumedh Ulhe