In my Webview I have many tag img(ImageView) and I want get event when click tag img in webview to show position of it in many image. How i do?
-
Possible duplicate of [android detect image click inside webview?](http://stackoverflow.com/questions/21409369/android-detect-image-click-inside-webview) – lewkka Nov 16 '15 at 11:33
3 Answers
First of all you need to enable javascript. like this,
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
then bind your javascript code with android code,
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();
}
}
then add javascriptInterface Method with webview, It will call your android method from any javascript method,
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
After that in your webview page just add onclick method on each tag img, and call javascript method from onclick event,
<img src="YOUR IMAGE SOURCE" id="your img positon OR anything" onClick="showAndroidToast(this.id)" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
for more reference please refer Android Webview

- 128
- 8
-
thank you I will try it. It mean with each tag img I must add string id = position and onclick = showAndroidToast (this.id) – Trần Văn Hòa Nov 17 '15 at 02:08
-
yes in id you can use whatever you want to use for further processing. – Swapnil Meshram Nov 17 '15 at 07:04
-
how I do to get position of img tag in webview? Ex my web have 5 tag img and I want when click tag img3 it show toast 3 – Trần Văn Hòa Nov 17 '15 at 10:19
-
-
sorry to bother after a while, but how I can show src of imageview? – Trần Văn Hòa Dec 08 '15 at 02:52
Use a Javascript Interface
to bind Javascript code (from html) to Android code.
eg:
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();
}
}
HTML (JS):
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
Add the JavaScriptInterface to your WebView
:
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
This is an example from the official documentation.
And this is another tutorial.

- 7,879
- 12
- 36
- 66
What I understand is that you need to find out when an image in the webview is clicked.
If so, you can use this : http://developer.android.com/reference/android/webkit/WebView.html#getHitTestResult()
and this :
http://developer.android.com/reference/android/webkit/WebView.HitTestResult.html
use it in your click listener and it should work!

- 89
- 8