4

I know this got asked often, but all solutions(, I found,) don't work for me.

What I have is a CardView with an OnClickListener making a Toast (#toast1). Inside the CardView there are multiple views as also a WebView.

As mentioned elsewhere, to pass through the click through the WebView to the CardView I have done following:

  • Set android:clickable="false" in WebView XML
  • insert following under CardView.setOnClickListener(...) WebView.setOnTouchListener( (view, event) -> true);

    I also replaced the lambda with an anonymous method, to see if its just this. No change.

What happens now is:

  • At the border and over the other views, the clickListener is triggered and the toast appears
  • Over the webView the clickListener isn't triggered.
  • Also put a toast (#toast2) in touchLstener of WebView before returning true, and it gets triggered.

What I expect:

  • Click will passed through WebView
  • With #toast2 added: First show #toast2 then #toast1

What is a bit confusing, that in documentation of OnTouchListener, the return is following:

True if the listener has consumed the event, false otherwise.

For me that means:

  • true: Don't pass click to below views, as listener consumed it
  • false: Pass click to below views, as listener didn't consumed it

But setting to false didn't change anything.

1 Answer 1

7

First of all I would suggest you to get familiar with android touch handling system - you can find a really good description in this answer. To sum it up: touch event propagation starts on top level of hierarchy, but actual handling of the touch event starts on the lowest level of view hierarchy. As for solution of your problem I may suggest to sublcass the parent of your WebView and override onInterceptTouchEvent in the following way:

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    return true;
}

This will instruct this parent view to intercept all touch events that would otherwise go to its children views, thus limiting the first level of touch processing to this view.

2
  • Thanks much. Always thought the problem to be in the webView not in the parent. With this the OnTouchListener don't have to be set anymore.
    – bvolkmer
    Commented Aug 12, 2015 at 8:40
  • @X4fyr the problem is actually in a WebView, since it consumes the touch event, but it's not that easy to handle this situation correctly within the WebView (however returning false in onTouchEvent or in dispatchTouchEvent may help) because it has it's own internal touch handling, so the easiest solution will be just to prevent the touch event from reaching the WebView
    – Chaosit
    Commented Aug 12, 2015 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.