Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent WebView from grabbing onClickListener

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.

like image 972
bvolkmer Avatar asked Mar 16 '23 03:03

bvolkmer


1 Answers

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.

like image 104
Chaosit Avatar answered Mar 24 '23 13:03

Chaosit