0

I'm searching for solution to disable copy/paste completely for EditText in android. I've tried no. of post and blogs but nothing working completely. Note that, it's working on some devices, but with same o/s on HTC device with with o/s 4.0.1, it's not working when user press on entered text three times once. Below is the sample class which I'm using for EditText, right now.

public class CustomEditText extends EditText{
    boolean canPaste() {
       return false;
    }
    @Override
    public boolean isSuggestionsEnabled() {
        return false;
    }
    public CustomEditText(Context context) {
        super(context);
        init();
    }
    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public CustomEditText(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        this.context = context;
        init();
    }

    private void init() {
        this.setCustomSelectionActionModeCallback(new ActionModeCallbackInterceptor());
        this.setLongClickable(false);
        this.setSelected(false);
    }

    private class ActionModeCallbackInterceptor implements ActionMode.Callback {
        public boolean onCreateActionMode(ActionMode mode, Menu menu) { return false; }
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; }
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; }
        public void onDestroyActionMode(ActionMode mode) {}
    }
} 

Note: I've check this solution EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event

Community
  • 1
  • 1
Pankaj
  • 833
  • 12
  • 35
  • You must be kidding? If enabled = false, how could any one edit that? – Pankaj Oct 09 '15 at 11:02
  • Are you tried using a TextWatcher? it should tell you when the user writes something, as far as i know it will give you 1 character if it was writed and a whole text if it was pasted. – Nanoc Oct 09 '15 at 11:05
  • Refer this. http://stackoverflow.com/questions/33036428/android-disable-copy-paste-in-edit-text-not-working-even-tried-some-solution It will help you. –  Oct 09 '15 at 11:06
  • @Nanoc, but in this case, user can copy text..right? – Pankaj Oct 09 '15 at 11:07
  • Yea... was thinking on avoid the user to be able to paste... maybe this helps you http://stackoverflow.com/questions/6275299/how-to-disable-copy-paste-from-to-edittext – Nanoc Oct 09 '15 at 11:08

2 Answers2

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 method **onCreateActionMode(ActionMode, Menu) will prevent ** the action mode from being started(Select All, Cut, Copy and Paste actions).

for more detail

Community
  • 1
  • 1
-1

Plz try below code.

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;
            }
        });
Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39
  • 1
    Don't try to guessing, I've wasted my lot of time over it. If someone set enabled = false, how could be edited then? – Pankaj Oct 09 '15 at 11:04