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.
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:
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:
isSuggestionsEnabled()
is public
, and may thus be overridden.canPaste()
is not, and thus must be hidden by introducing a function of the same name in the derived 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With