Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - prevent edittext copy/paste and double tap select all [duplicate]

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.

like image 997
Hunter23 Avatar asked Jan 21 '13 21:01

Hunter23


1 Answers

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:

  • 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.

like image 53
CJBS Avatar answered Sep 19 '22 16:09

CJBS