5

I want to disable the long click on webView so I cant select the text in it, and I use three ways to do that but nothing work:

1) android:longClickable="false"

2) webView.setLongClickable(false);

3) webView.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });

So any help please.

Mahmoud Jorban
  • 952
  • 4
  • 13
  • 25

7 Answers7

14

To disable long clicks you need to first enable long click setLongClickable(true); then the setOnLongClickListener, and return nothing:

webview.setLongClickable(true);
webview.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        return true;
        }
    });

If you only need to disable text selection it's better to add the following CSS to your webpage and it will disable text selection:

body {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

Check the Mozilla documentation here.

sharshi
  • 963
  • 11
  • 16
2

This worked for me, as shown at https://stackoverflow.com/a/12793740/5035343

mWebView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
    return true;
}
});
mWebView.setLongClickable(false);

And to remove the viberation caused by the long click, you can try this.

mWebView.setHapticFeedbackEnabled(false);
Community
  • 1
  • 1
Esther
  • 68
  • 4
0

Try this:

   public void SelectText(View v) {
        try {
            KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                    KeyEvent.KEYCODE_ESCAPE, 0, 0);
            shiftPressEvent.dispatch(v);
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }

or :

public boolean onTouchEvent(MotionEvent "ACTION_DOWN") {
    try {
        KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
        shiftPressEvent.dispatch(webview);
    } catch (Exception e) {
        throw new AssertionError(e);
    }
    return false;
}
Sardor Dushamov
  • 1,665
  • 3
  • 17
  • 44
0

Try this:

webView.setOnLongClickListener(null);
vinoth
  • 225
  • 3
  • 9
0

This method is useful for me, Check it may be help you guys.

1) Just use this webView.setLongClickable(false);

2) use in html with css:

body {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

3) replace body tag with this in html <body oncontextmenu="return false;">

0

use this for kotlin

binding.webViewId.setOnLongClickListener(OnLongClickListener { true })

or

webViewId.setOnLongClickListener(OnLongClickListener { true })
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0

This so far working with me

webView.setOnLongClickListener { _ -> true }
Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103