3

I'm using WebView in my Android app. I want to run Push Notification when users click on a button. Can anyone help me to provide java code to handle the onClick button of WebView.

0

1 Answer 1

3

You can achieve this in Android by using JavascriptInterface

Create class in your Activity, Name it as JavaScriptInterface class. create method
onButtonClick() inside the class

public class JavaScriptInterface {
        Context mContext;

        /**
         * Instantiate the interface and set the context
         */
        JavaScriptInterface(Context c) {
            mContext = c;
        }


        @JavascriptInterface
        public void onButtonClick() {
           // Handle your code

        }

    }

Add this class reference to WebView like below.

 webview.addJavascriptInterface(new JavaScriptInterface(this), "Android");

And in your WebPage you have to call onButtonClick() method in WebPage Button click

Below is code

<html>
    <body>
    <a onClick="onButtonClick()"> Click me, i am JS Button </a>
    </body>
</html>

This will call your Activity onButtonClick() method.

Hope this will help for more clarification you check this link

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.