Android passing touch event to WebView's inner content

15,143

Solution 1

You need to set, inside the main activity's OnCreate(), an OnTouchListener() with a 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;
    }
});

Solution 2

Don't use the 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);
}
Share:
15,143
pat
Author by

pat

mobile developer

Updated on June 22, 2022

Comments

  • pat
    pat almost 2 years

    I have a webview that I'm setting an onTouchListener on in order to capture swipe events on the actual view.

    I also set a WebViewClient on the WebView in order to override Url Loading when clicking on certain links inside the webview.

    The problem is the OnTouch handler always receives the action first and even if I return false (when not a swipe) it does not pass the touch event to the inner html content and thus the link is never actually clicked. If I remove the onTouchListener it works fine. Is it possible to pass the touch event to the content somehow?

  • Gowthaman M
    Gowthaman M almost 7 years
    When i load webview that time only its working.after loading webview when i touch it's not working....view.officeapps.live.com/op/view.aspx?src=http://‌​… help me sir
  • Gowthaman M
    Gowthaman M almost 7 years
    Its not working for me....view.officeapps.live.com/op/view.aspx?src=http://… help me sir
  • Black
    Black almost 5 years
    The function onTouchEvent does not exists