65

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
janoliver
  • 7,744
  • 14
  • 60
  • 103
  • For those curious to handle (e.g. disable) all touch events through a custom `View.OnTouchListener`, [see this related question](http://stackoverflow.com/questions/3853794/disable-webview-touch-events-in-android). – Paul Lammertsma Sep 12 '11 at 22:22

7 Answers7

142

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);
Paul Chu
  • 1,249
  • 3
  • 19
  • 27
Samuel
  • 9,883
  • 5
  • 45
  • 57
44

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

Example CSS to disable selection:

* {
   -webkit-user-select: none;
}
Error 454
  • 7,255
  • 2
  • 33
  • 48
lhk
  • 449
  • 4
  • 2
9

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.

janoliver
  • 7,744
  • 14
  • 60
  • 103
  • My problem remains: The `event.preventDefault()` call in the touchstart event also prevents the normal cursor mode input/selection (on html textinput elements). – Nappy Apr 08 '11 at 15:31
  • that is true... Maybe switching off all long touch events on the android layer and removing the preventDefaults works? – janoliver Apr 12 '11 at 06:22
  • the behaviour is confusing me.. returning true for every onTouch event seems to work, but when I only select certain (long) move and down events to skip further propagation, I run into states, where I do not get any event until text selection is cancelled (e.g.) input element changed.. text selection seems also be triggered between the down and the (future) up event. maybe I am missing another event type where I can cancel it.. – Nappy Apr 19 '11 at 10:16
  • I've found that this breaks any scrolling in the browser. For example, if you put your finger on the `selector` element and try to scroll up and down, nothing will happen. – EMMERICH Jun 21 '11 at 09:02
  • @EMMERICH, I ran into same problem, it broke scrolling. Is there any solution to prevent text selection in native way? – fifth Aug 01 '12 at 09:49
  • Thanks for this info - I was having a different problem - trying to prevent android from popping up a selector box while the html selector was disabled by my code (using logic, not the disabled state), and your method of catching 'touchstart' did the trick – Yaakov Aug 02 '13 at 02:19
6

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();
        }

    });
crusherjoe
  • 61
  • 1
  • 1
  • 1
    I need to have the copy/paste feature in my webview. I used setWebChromeClient to use onProgressChanged method for progress bar updates. This disabled the copy/paste feature. Adding the onSelectionStart method enabled copy/paste again. Thanks a lot for this hidden gem! – MediumOne Jun 28 '12 at 09:55
  • This method isn't in the API, what are you guys talking about? http://developer.android.com/reference/android/webkit/WebChromeClient.html – Christopher Perry Aug 29 '12 at 21:20
  • This also confuses me, what Android system are you talking about? – neevek Dec 12 '12 at 08:36
  • They are referring to a "hidden" API method: https://bugzilla.wikimedia.org/show_bug.cgi?id=31484#c15 hence why it does not show up in the API docs for WebChromeClient class. – Maks Feb 06 '13 at 02:07
  • This workaround only works until SDK 2.3.7 r1. For newer SDK you can't use WebChromeClient or don't use Copy/Paste on 2.3.x mobiles. See http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/webkit/WebChromeClient.java/ and http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.7_r1/android/webkit/WebChromeClient.java/ – pjw Aug 19 '13 at 17:17
  • Would this also help with long clicking on links in order to open them in the default web browser? – android developer Aug 03 '15 at 07:45
0

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.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • But I want to implement a longclick listener in javascript as well. By overriding the onTouchListener I would turn off all long clicks on the view. How can it be, that one can not turn off text selection? I think this is a pretty basic thing to want to do. – janoliver Feb 24 '11 at 16:54
  • Got it. Try this: http://stackoverflow.com/questions/923782/disable-the-text-highlighting-magnifier-on-touch-hold-on-mobile-safari-webkit – Peter Knego Feb 24 '11 at 17:13
  • does not seem to have any effect in android. – janoliver Feb 24 '11 at 17:53
  • but with android 4.4 default selection also coming – Ravi Dec 02 '13 at 04:53
  • this would disable scrolling. – neobie Feb 10 '23 at 08:10
0

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;
   }
}
0

For kotlin i found the following to work:

webView.isLongClickable = false
DarkPh03n1X
  • 600
  • 1
  • 7
  • 17