Disable Webview Touch Events in Android

Disable WebView touch events in Android

mWebView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});

Disables all touch events on a WebView because the touch listener is executed before the default touch behavior of the WebView. By returning true the event is consumed and isn't propagated to the WebView.

Using android:clickable="false" does not disable touch events.

How to disable touch event?

The way to ignore a touch event on a webview is quite simple, just do :

mWebView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false; //returns true if you wan't also ignore the js touch events
}
});

The thing is, I'm not sure the triggered event are from the webview or another view, you need to define it so you can set this same OnTouchListener on the appropriate view.

Android disable passing events to underlying webview

You can attach a View.OnTouchListener on your TextView that returns true to prevent propagating touches to the underlying WebView.

findViewById(R.id.textview).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// return TRUE since we want to consume the event
return true;
}
});

Note that this will prevent all touches on your WebView: scrolls and clicks.

How to disable the content touch in WebView without afecting the double tap zoom .

If you only want to prevent navigations (user following links) then you need to use the shouldOverrideUrlLoading API:

webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});


Related Topics



Leave a reply



Submit