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.
1 Answer
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