How can I detect whether Android WebView consumed a touch event? onTouchEvent
always returns true and WebViewClient
's onUnhandledInputEvent
is never triggered.
I have multiple WebViews inside a TwoDScrollView. As its name suggests, the TwoDScrollView can be scrolled both vertically and horizontally. The contents of TwoDScrollView can be zoomed in / out. When the user drags his finger or uses pinch-to-zoom, I want to dispatch the touch event to:
I have partially achieved this by using the canScrollHorizontally
and canScrollVertically
methods. But these methods only work for "native scrolling". However, in some cases, some JavaScript inside the WebView consumes the touch event, for example Google Maps. In this case, the methods return false. Is there any way to find out whether the WebView's contents consumes the touch events, i.e. is scrollable / zoomable? I cannot change the contents of the WebView, therefore my question is different from this one.
I have considered checking touch handlers by executing some JavaScript inside the Webview by the evaluateJavaScript
method, but according to this answer there is no easy way to achieve this and also the page can have some other nested iframes. Any help will be appreciated.
onTouchEvent
and read super.onTouchEvent()
which always returns true, no matter what.canScrollHorizontally
and canScrollVertically
only partially solve this problem, as mentioned aboveonScrollChanged
isn't useful eitherWebViewClient.onUnhandledInputEvent
is never triggeredevaluateJavaScript
, but it is a very complicated and ugly solutionDebug.startMethodTracing
. I found out it is propagated as follows: android.webkit.WebView.onTouchEvent
com.android.webview.chromium.WebViewChromium.onTouchEvent
com.android.org.chromium.android_webview.AwContents.onTouchEvent
com.android.org.chromium.android_webview.AwContents$AwViewMethodsImpl.onTouchEvent
com.android.org.chromium.content.browser.ContentViewCore.onTouchEventImpl
nativeOnTouchEvent
and I don't know what further happens with it. Anyway, onTouchEvent
always returns true and even if it was possible to find out somewhere whether the event was consumed or not, it would require using private methods which is also quite ugly.I don't need to know how to intercept touch events sent to WebView, but whether the WebView is consuming them, i.e. is using them for doing anything, such as scrolling, dragging etc.
Use btn. setEnabled(false) to temporarily disable it and then btn. setEnabled(true) to enable it again.
The touch event is passed in as a MotionEvent , which contains the x,y coordinates, time, type of event, and other information. The touch event is sent to the Window's superDispatchTouchEvent() . Window is an abstract class. The actual implementation is PhoneWindow .
To make sure that each view correctly receives the touch events intended for it, override the onInterceptTouchEvent() method.
According to this issue report, not possible. If the web code is under your control, you can implement some JavaScriptInterface to workaround this. If not, I am afraid there is no solution here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With