TL;DR
How can I detect whether Android WebView consumed a touch event? onTouchEvent
always returns true and WebViewClient
's onUnhandledInputEvent
is never triggered.
Detailed description
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:
- WebView if its content is scrollable / zoomable (i.e. only the inside of the WebView will scroll / zoom)
- TwoDScrollView if the above condition is false (all contents of the TwoDScrollView will scroll / zoom)
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.
What I've already tried
- I overrode WebView's
onTouchEvent
and readsuper.onTouchEvent()
which always returns true, no matter what. canScrollHorizontally
andcanScrollVertically
only partially solve this problem, as mentioned aboveonScrollChanged
isn't useful eitherWebViewClient.onUnhandledInputEvent
is never triggered- I considered using JavaScript via
evaluateJavaScript
, but it is a very complicated and ugly solution - I tried to trace the MotionEvent by
Debug.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
- According to ContentViewCore's source code the touch event ends up in a native method
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.
Note
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.