9

I am looking for a solution to disable the long press edit menu(copy/paste) and also the double-tap select-all function for an edittext box. I have read through a few solutions on here to this issue in similar fashion, however, need to go a step further.

For this app I have a requirement to prevent the user from using the copy/paste functions on my app and need a way to disable this functionality that will work on 2.3 API as well as when used on a newer tablet. I am testing with a NexusOne phone device and a Galaxy Tablet. (minSDK is 8, target is 10)

Currently I am using this (as example): edittextPassword.setLongClickable(false); -disables the popup edit menu, Great!

This method does work on the phone device to prevent the edit menu popup. On the tablet, this is also disabled from the long-press action. The tablet however has newer functionality of a double-tap which will select-all text and open the edit menu. Is there a way for an older API to cancel/trap/disable a newer API feature or prevent the double-tap gesture?


Update: Using a combination of setLongClickable(false) and setOnTouchListener connected to a GestureDetector (thank you Nikola for suggestion) I am able to trap/cancel the double click and long click edit menus from opening.

The next part to figure out is this ... on Samsung phones (Galaxy S, not sure about other devices at the moment), when you tap into a field, you get a cursor and a floating cursor pointer button (proper name??). Clicking on this pointer button is another method to open the edit menu. Anyone know how to disable this one? My only workaround left is to clear the Clipboard on BeforeTextChanges and AfterTextChanges using a TextWatcher. This ensures that even if you get to the Copy/Paste menu, the clipboard is cleared and there is nothing to paste.

Hunter23
  • 195
  • 4
  • 11
  • "Currently I am using this: edittextPassword.setLongClickable(false); -disables the popup edit menu, Great!" -- I will be surprised if that works on all devices. More than one manufacturer has modified how `EditText` works with respect to these sorts of operations. – CommonsWare Jan 21 '13 at 21:22
  • 4
    Please also consider your users. I am constantly frustrated by EditTexts for passwords that do not allow paste since I use randomly generated strong passwords (Dashlane) for all my accounts and typing those in is so difficult that I usually have to change the password to something easy, type it in, then change the password back to a strong one. It sucks... – Simon Jan 21 '13 at 21:44
  • 1
    Use GestureListener and return true everytime there is double tap/long press. It will prevent the system to react accordingly if the event wasn't handled. – Nikola Despotoski Jan 22 '13 at 02:24
  • Please don't annoy your users - and remember you can't stop them from simply taking a picture of the phone with another one. – Chris Stratton Jan 22 '13 at 21:30
  • 2
    I certainly understand the user side, and have already had the argument over user annoyance, photo copying, manually copying data from one app into another and other user 'workarounds'. And numerous conversations about Android convention dos and donts. However, regardless of all that, for this app I have a specific requirement to ensure my app does all it can to prevent copy/paste from within itself. If that were not the case, then of course I wouldn't be looking for a solution. – Hunter23 Jan 23 '13 at 14:29
  • Try out the accepted answer for [this](http://stackoverflow.com/questions/6275299/how-to-disable-copy-paste-from-to-edittext) question. – Praga Jul 09 '14 at 06:39

1 Answers1

1

Regarding the 'floating cursor pointer button', which is called a cursor controller, or handle, and the context menu that appears when it's clicked, yes, there is a way to disable it. I'm assuming that you're referring to this:

Text selection handle with paste menu

In addition to the setLongClickable(false) and setOnTouchListener code, the fix lies in preventing PASTE/REPLACE menu from appearing in the show() method of the (non-documented) android.widget.Editor class. Before the menu appears, a check is done to if (!canPaste && !canSuggest) return;. The two methods that are used as the basis to set these variables are both in the EditText class:

A more complete answer is available here.

As @CommonsWare mentions, individual device manufacturers may have changed the default AOSP behaviour of the EditText control, so testing is necessary.

Community
  • 1
  • 1
CJBS
  • 15,147
  • 6
  • 86
  • 135