How to disable copy/paste from/to EditText

123,037

Solution 1

If you are using API level 11 or above then you can stop copy,paste,cut and custom context menus from appearing by.

edittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            public void onDestroyActionMode(ActionMode mode) {                  
            }

            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }
        });

Returning false from onCreateActionMode(ActionMode, Menu) will prevent the action mode from being started(Select All, Cut, Copy and Paste actions).

Solution 2

Best method is to use:

etUsername.setLongClickable(false);

Solution 3

You can do this by disabling the long press of the EditText

To implement it, just add the following line in the xml -

android:longClickable="false"

Solution 4

I am able to disable copy-and-paste functionality with the following:

textField.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        return false;
    }

    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
        return false;
    }

    public boolean onActionItemClicked(ActionMode actionMode, MenuItem item) {
        return false;
    }

    public void onDestroyActionMode(ActionMode actionMode) {
    }
});

textField.setLongClickable(false);
textField.setTextIsSelectable(false);

Hope it works for you ;-)

Solution 5

Kotlin solution:

fun TextView.disableCopyPaste() {
    isLongClickable = false
    setTextIsSelectable(false)
    customSelectionActionModeCallback = object : ActionMode.Callback {
        override fun onCreateActionMode(mode: ActionMode?, menu: Menu): Boolean {
            return false
        }

        override fun onPrepareActionMode(mode: ActionMode?, menu: Menu): Boolean {
            return false
        }

        override fun onActionItemClicked(mode: ActionMode?, item: MenuItem): Boolean {
            return false
        }

        override fun onDestroyActionMode(mode: ActionMode?) {}
    }
}

Then you can simply call this method on your TextView:

override fun onCreate() {
    priceEditText.disableCopyPaste()
}
Share:
123,037
rDroid
Author by

rDroid

Updated on March 07, 2022

Comments

  • rDroid
    rDroid about 2 years

    In my application, there is a registration screen, where i do not want the user to be able to copy/paste text into the EditText field. I have set an onLongClickListener on each EditText so that the context menu showing copy/paste/inputmethod and other options does not show up. So the user won't be able to copy/ paste into the Edit fields.

     OnLongClickListener mOnLongClickListener = new OnLongClickListener() {
    
            @Override
            public boolean onLongClick(View v) {
                // prevent context menu from being popped up, so that user
                // cannot copy/paste from/into any EditText fields.
                return true;
            }
        };
    

    But the problem arises if the user has enabled a third-party keyboard other than the Android default, which may have a button to copy/paste or which may show the same context menu. So how do i disable copy/paste in that scenario ?

    Please let me know if there are other ways to copy/paste as well. (and possibly how to disable them)

    Any help would be appreciated.

    • BitBank
      BitBank over 12 years
      If the "paste" operation comes from an IME, then you have no standard way of distinguishing it from normal keystrokes. One idea to try is to measure the time between each character's arrival and if the time is too short, then the characters are coming from a "paste" operation.
    • rDroid
      rDroid over 12 years
      seems to be dirty soloution! worth a look though.
    • Azay Gupta
      Azay Gupta almost 5 years
      use android:longClickable="false"
    • Kraigolas
      Kraigolas over 3 years
      The conclusion for everyone seems to be that: you really can't do it nicely. However, for my personal purposes, I want to disable paste because I can't handle certain characters being present, and paste can allow them into my EditText. A solution then is to add a text changed listener, and in the afterTextChanged method, remove those characters if they're there. You can add multiple listeners, and thus create one that prevents the text from being too long, invalid characters, etc. This is not preferable. But if anyone was looking for a half decent workaround, I think this is it.
  • Jono
    Jono over 11 years
    what about api level below 13?
  • Petr Prazak
    Petr Prazak over 10 years
    Dont understand either comments, this sample works api11+, pre-api11 there wasn't copy and paste IIRC
  • lomza
    lomza over 10 years
    Or, just in xml android:longClickable="false" :)
  • void
    void about 10 years
    Paste button will appear in case of tapping on blue cursor indicator.
  • void
    void about 10 years
    Not working for me.Paste button will appear in case of tapping on blue cursor indicator.
  • Kevin Grant
    Kevin Grant about 10 years
    This will certainly prevent the view from being long clickable, but the editing controls can also be requested by double tapping on text, which means that this solution is not complete. Keep this in mind for your purposes.
  • Sheepdogsheep
    Sheepdogsheep about 10 years
    This worked for me, I just had to add @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  • Oleg Vaskevich
    Oleg Vaskevich almost 10 years
    Also, keyboard shortcuts could still work (Ctrl+C) w/external keyboards.
  • Android Killer
    Android Killer almost 10 years
    Also not working for me. On Double tapping the menu of copy-paste is showing.
  • CJBS
    CJBS about 9 years
    This does exactly the same as the half-solution provided by stackoverflow.com/a/22756538/3063884. See the code: github.com/neopixl/PixlUI/blob/master/Library/src/com/neopix‌​l/… ... This approach still doesn't prevent the text selection handler from showing "PASTE" if there's text in the clipboard.
  • rDroid
    rDroid about 9 years
    problem was that my app user has a third party keyboard which has a copy and paste button.
  • FireZenk
    FireZenk over 8 years
    This is the CORRECT and COMPLETE solution
  • has19
    has19 almost 8 years
    this doesn't working anymore on android 6.0 ,check this answer stackoverflow.com/questions/27869983/…
  • Palejandro
    Palejandro over 7 years
    This solution is good(but not perfect), unfortunately, in my case it caused NPE in android part of the code (sometimes, during "monkey testing"), so I had to find another solution, see my answer below
  • Paul Wintz
    Paul Wintz about 7 years
    This does not work on Ice Cream Sandwich because the clipboard options can be opened by double tapping text, as well as long tapping.
  • user924
    user924 over 6 years
    but I still want to select text, just without widget
  • Nikola
    Nikola over 6 years
    another problem is you can select the text by double tap and it shows copy/paste again
  • Skye Hoefling
    Skye Hoefling about 6 years
    This is the exact same solution I ended up with based on the other answers above. This should be marked as the correct solution since it handles the edge cases the others do not
  • Sagar Jethva
    Sagar Jethva over 5 years
    It's work only when EditText is not empty, It's Doest not work when edittext have some texts.
  • Alex Semeniuk
    Alex Semeniuk over 5 years
    Good one. Alternatively, just set android:longClickable="false" in XML
  • Mehul Kanzariya
    Mehul Kanzariya over 5 years
    This option blocks the copy but you can still paste by clicking on the cursor.
  • Hitesh Sarsava
    Hitesh Sarsava over 5 years
    @Humble coder the above solution is not working moto g5 mobile and in OS oreo. the solution for all devices is use below method : edittext.setCustomInsertionActionModeCallback();
  • Ragavendra M
    Ragavendra M over 5 years
    It will never Work
  • Abdul Mateen
    Abdul Mateen over 4 years
    Hi, I'm using this approach, but I'm getting Type mismatch error with this description Required:ActionMode.Callback! Found: on this part object: ActionMode.Callback. Any idea why it might not be working?
  • Richa
    Richa about 4 years
    In some devices instead of Paste Clipboard option is visible, acts as paste only. i checked links but i am able to prevent paste but not clipboard. any idea ?
  • Richa
    Richa about 4 years
    can you tell for Clipboard disable
  • Deepak Borade
    Deepak Borade over 3 years
    i used this code it prevent from clipboard,copy option.Thank you
  • David Rector
    David Rector almost 3 years
    This fails because auto-correct, at least on my device, sometimes wants to replace characters at the same time it adds the newly typed characters. When this happens, this code thinks it's a paste and the whole auto-correcting (underlined) word is erased. The fix is to see if the source length is the destination length plus one - in that case, it's ok to accept the characters. But this is a kludge and also has the effect of disabling the "tap a word" to do auto-complete since that is exactly like a paste operation.
  • Amos
    Amos almost 3 years
    this solution is more comprehensive
  • DebauchMode
    DebauchMode over 2 years
    Try object : android.view.ActionMode.Callback instead