Is it possible to detect button click on web page which loaded in an Android WebView
? I am trying to display an image when particular button is clicked. Is there some way to achieve this?
Asked
Active
Viewed 152 times
0

kenny_k
- 3,831
- 5
- 30
- 41

Stephen Balaji
- 195
- 1
- 2
- 11
-
1Possible duplicate of [Android WebView - Intercept clicks](https://stackoverflow.com/questions/3250034/android-webview-intercept-clicks) – Andre Classen Oct 21 '19 at 08:50
-
its not working – Stephen Balaji Oct 21 '19 at 13:09
1 Answers
-1
You can use JavascriptInterface annotation to interact with any web page in webview.
Define a method annotated with JavascriptInterface
public class WebAppInterface { Context mContext;
// Instantiate the interface and set the context
WebAppInterface(Context c) {
mContext = c;
}
// Show a toast from the web page
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
and add instance of this class to webview
webView.addJavascriptInterface(new WebAppInterface(this), "AndroidInterface");
and call method from javascript in web page.
// this is a javascript code
AndroidInterface.showToast('Hello');

Gautam Singh
- 37
- 4