Skip to content Skip to sidebar Skip to footer

How To Detect Single Tap On Webview

How to detect single Tap on the WebView. I am displaying WebView as a ViewPager page. So, I can't use OnTouchListener. And OnClickListener doesn't work on WebView.

Solution 1:

If you had a link to get the event you can use something like this with shouldOverrideUrlLoading:

webView.setWebViewClient(newWebViewClient() {

            @OverridepublicvoidonReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
                try {
                    String host = null;
                    if (null != url && null != (host = Uri.parse(url).getHost())) {
                        if (host.equals("myclikableUrl")) {
                            clickEvent();
                        } 

                        returntrue;
                    } else {

                        returnfalse;
                    }
                } catch (Exception e) {
                    returnfalse;
                }
            }.....
});

Solution 2:

Try to crete your own class that extends WebView and here override dispatchTouchEvent(MotionEvent ev). Also you could add GestureDetector and catch single tap on webview http://developer.android.com/training/gestures/detector.html

Post a Comment for "How To Detect Single Tap On Webview"