How to disable long click on WebView in android

11,143

Solution 1

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.

Solution 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);
Share:
11,143
Mahmoud Jorban
Author by

Mahmoud Jorban

Android developer

Updated on June 04, 2022

Comments

  • Mahmoud Jorban
    Mahmoud Jorban almost 2 years

    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.