3
votes

Is it possible to capture touch events over a WebView in the activity that contains it, without loosing link functionality?

Consider a WebView showing a webpage with links. If the user taps on a link I would like the WebView to handle the touch event. If the user taps somewhere else I would like the activity to handle the touch event.

How can this be done?

4
curious as to the implementation of this question as well. The browser (i assume) is nothing more than a webview with the user interface overlaid on top of it, which can include buttons. Also, with a mapview, the zoom in/zoom out buttons are simply overlaid on top of the mapview. In a broader sense, I guess you are interested in overlaying buttons over any view (unless you are specifically having quirks with a webview) - Gimbl
@Gimbl I completely rewrote the question. Would have deleted it and posted a new one if I it were possible. - hpique

4 Answers

0
votes

Yes, it is.

(I can't elaborate more on this unless you are more specific on your question.)

0
votes

If i understand your question correctly it'd seem difficult to interpret whether something is a link or not in the onTouchEvent() since all it knows about is X,Y coordinates (not html). However, off the top of my head it seems you could probably use the javascript binding piece in WebView to have javascript decide if the something is a link or not and act accordingly. Again, I haven't done this before nor tested it...just thinking out loud.

0
votes

When initializing your WebView, try:

mWebView.setWebViewClient(new WebViewClientOverrider());

Then create a private class:

private class WebViewClientOverrider extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        //TODO handle the link  
        return true;
    }
}

Lastly, replace the TODO handle the link line with your own code for handling the link selection.

0
votes

Or you can monitor the stack trace for:

android.webkit.CallbackProxy.uiOverrideUrlLoading()

See http://www.javapractices.com/topic/TopicAction.do?Id=78 for how to determine your stack trace from a throwable (created in an overriden WebView.loadUrl() method).