Android sends event touch to internal content of WebView

I have a web view in which I set onTouchListener to capture swipe events on the actual view.

I also install WebViewClient in WebView to override Url loading when clicking on specific links inside webview.

The problem is that the OnTouch handler always takes the action first, and even if I return false (if not a miss), it does not send a touch event to the internal html content, and thus the link will never be clicked. If I remove onTouchListener, it works fine. Is it possible to somehow transfer the touch event to the content?

+4
source share
2 answers

You need to set OnCreate () OnTouchListener () inside the main action using requestFocus ():

mWebView = (MyWebView) findViewById(R.id.webview);

mWebView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                if (!v.hasFocus()) {
                    v.requestFocus();
                }
                break;
        }
        return false;
    }
});
+5
source

Do not use OnTouchListener, but instead override OnTouchEvent in your WebView:

@Override
public boolean onTouchEvent(MotionEvent event) {

    // do your stuff here... the below call will make sure the touch also goes to the webview.

    return super.onTouchEvent(event);
}
0
source

All Articles