Android: Disable text selection in a webview

AndroidWebview

Android Problem Overview


I am using a webview to present some formatted stuff in my app. For some interaction (which are specific to certain dom elements) I use javascript and WebView.addJavascriptInterface(). Now, I want to recognize a long touch. Unfortunately, onLongTouch, in Android 2.3 the handles for text selection are displayed.

How can I turn off this text selection without setting the onTouchListener and return true? (Then, the interaction with the "website" doesn't work anymore.

Android Solutions


Solution 1 - Android

This worked for me

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

I have not tested, if you don't want the vibration caused by the long click, you can try this:

mWebView.setHapticFeedbackEnabled(false);

Solution 2 - Android

Setting webkit css property -webkit-user-select to none would solve the problem.

Example CSS to disable selection:

* {
   -webkit-user-select: none;
}

Solution 3 - Android

I figured it out!! This is how you can implement your own longtouchlistener. In the function longTouch you can make a call to your javascript interface.

var touching = null;
$('selector').each(function() {
	this.addEventListener("touchstart", function(e) {
		e.preventDefault();
		touching = window.setTimeout(longTouch, 500, true);
	}, false);
	this.addEventListener("touchend", function(e) {
		e.preventDefault();
		window.clearTimeout(touching);
	}, false);
});

function longTouch(e) {
	// do something!
}

This works.

Solution 4 - Android

It appears that cut/paste via long press is turned off if you used

    articleView.setWebChromeClient(new WebChromeClient(){...})

See https://bugzilla.wikimedia.org/show_bug.cgi?id=31484

So if you are using setChromeClient and you WANT to have long click to start copy/paste, the do the following:

    webView.setWebChromeClient(new WebChromeClient(){

        [.... other overrides....]

        // @Override
        // https://bugzilla.wikimedia.org/show_bug.cgi?id=31484
        // If you DO NOT want to start selection by long click,
        // the remove this function
        // (All this is undocumented stuff...)
        public void onSelectionStart(WebView view) {
            // By default we cancel the selection again, thus disabling
            // text selection unless the chrome client supports it.
            // view.notifySelectDialogDismissed();
        }

    });

Solution 5 - Android

It seems that the only option is to set onTouchListener and write your own code to detect long-click. Then return true if it's a long-click and false otherwise.

Solution 6 - Android

An alternative solution is to subclass WebView and Override performLongClick as bellow:

public class AdvanceWebView extends WebView {
   //Add constructors...
   @Override
   public boolean performLongClick() {
   return true;
   }
}

Solution 7 - Android

For kotlin i found the following to work:

webView.isLongClickable = false

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjanoliverView Question on Stackoverflow
Solution 1 - AndroidSamuelView Answer on Stackoverflow
Solution 2 - AndroidlhkView Answer on Stackoverflow
Solution 3 - AndroidjanoliverView Answer on Stackoverflow
Solution 4 - AndroidcrusherjoeView Answer on Stackoverflow
Solution 5 - AndroidPeter KnegoView Answer on Stackoverflow
Solution 6 - AndroidVahid habibi nasabView Answer on Stackoverflow
Solution 7 - AndroidDarkPh03n1XView Answer on Stackoverflow