I am looking for a method to disable touch on a WebView so user's are not able to click on html links but keep the zooming and scrolling functions of the WebView.
Asked
Active
Viewed 3,973 times
4 Answers
2
the code below allow you scroll but when click at the link it won't go to next page.
webView.setLongClickable(false);
webView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
return arg1.getAction() == MotionEvent.ACTION_UP;
}
}
);

rattisuk
- 407
- 5
- 7
1
Webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});

Adi
- 5,089
- 6
- 33
- 47

Juan Manuel
- 239
- 2
- 4
-
This will have no effect on stopping the user's click on the WebView. This just lets my app navigate to the link without going to the default browser. – elude Sep 17 '12 at 01:40
-
1I think the answer is correct, except you want to return **true** not false to indicate that you will be handling the url load in your host app. – Maks Sep 17 '12 at 03:58
-
Returning true will just open whichever link is pressed in the default browser. – elude Sep 17 '12 at 15:44
1
I used rattisuk's answer at fist but realized that if you click somewhere, the click gets intercepted but turns into a long click, making the phone vibrate and also poping some potential edition menu if you clicked on some text field. Also whenever pressing on changeable UI elements in the webview (clicking on a link or a button for example), those UI elements would change appearance. To prevent that, here is my improved solution :
// Disable the haptic feedback so that handling the long click doesnt make the phone vibrate
webview.setHapticFeedbackEnabled(false);
// Intercept long click events so that they dont reach the webview
webview.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(final View v) {
return true;
}
});
// Intercept any touch event not related to scrolling/zooming
webview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View v, final MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Change ACTION_DOWN location to outside of the webview so that it doesnt affect
// pressable element in the webview (buttons receiving PRESS" will change appearance)
event.setLocation(webview.getWidth() + 1, webview.getHeight() + 1);
}
// Intercept any "up" event
return event.getAction() == MotionEvent.ACTION_UP;
}
});

androidseb
- 1,257
- 2
- 14
- 17
-
Thanks @androidseb it helped me with disabling all touch functionality but scrolling – Reshma Oct 01 '14 at 09:14
0
Here is my solution. It does not let a user trigger javascript click events
public class NonClickableWebView extends WebView {
private GestureDetectorCompat gestureDetector;
private boolean isSingleTap = false;
public NonClickableWebView(Context context) {
super(context);
gestureDetector = new GestureDetectorCompat(context, new GestureDetector());
}
public NonClickableWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
gestureDetector = new GestureDetectorCompat(context, new GestureDetector());
}
public NonClickableWebView(Context context, AttributeSet attrs) {
super(context, attrs);
gestureDetector = new GestureDetectorCompat(context, new GestureDetector());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isSingleTap = false;
}
gestureDetector.onTouchEvent(event);
if (isSingleTap && event.getAction() == MotionEvent.ACTION_UP) {
return true;
}
return super.onTouchEvent(event);
}
private class GestureDetector extends android.view.GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent e) {
isSingleTap = true;
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
isSingleTap = true;
return false;
}
}
}

Michael Roland
- 39,663
- 10
- 99
- 206

Buckstabue
- 283
- 1
- 13