Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling text selection in PhoneGap

Is it possible to disable text selection to make a PhoneGap app more similar to normal native app?

Something like this:

document.onselectstart = function() {return false;}

or:

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

Or many other things don't work.

like image 865
stmn Avatar asked Aug 05 '12 22:08

stmn


4 Answers

Putting it on html, not *, works for me:

html {
    -webkit-user-select: none;
}
like image 62
ThinkingStiff Avatar answered Nov 10 '22 19:11

ThinkingStiff


I looked all over for help on this. This finally worked for me.

public class MainActivity extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.loadUrl("file:///android_asset/www/index.html");

        super.appView.setOnLongClickListener(new View.OnLongClickListener() {

            public boolean onLongClick(View v) {
                return true;
            }
        });
    }
}

The setOnClickListener is what does the magic. Make sure you put this AFTER your call to super.loadUrl.

Of course, this will disable text selection for your entire app, but I'm OK with that, and I don't have any other way around it for now.

I'm not sure of the complete implications of this yet, but I do make use of the JqueryMobile event "taphold", and it still works fine. I believe this works by handling the long click on the appView (which hosts your HTML app) and preventing it from bubbling up.

like image 32
habermanm Avatar answered Nov 10 '22 19:11

habermanm


this will work too.

<body oncontextmenu="return false" ondragstart="return false" 
onselectstart="return false">
like image 9
nargil Avatar answered Nov 10 '22 18:11

nargil


As well as what ThinkingStiff mentioned I also use the following to remove any highlighting/copy & paste

-webkit-touch-callout: none;
-webkit-tap-highlight-color: rgba(0,0,0,0); 
like image 8
mattdryden Avatar answered Nov 10 '22 19:11

mattdryden