Search code examples
androidandroid-edittextcopy-paste

Android: How to TOTALLY disable copy and paste function in Edittext


I am quite new to Android developing area and recently I hv encountered a tough problem.

I was trying to make a Edittext which should NOT ALLOW user to copy content from or paste content to it. I hv googled a lot and find there seems to be 2 popular ways of doing so:

1st way, to set it in the layout file:

android:longClickable="false"

2nd way, to programmatically set it:

myEdittext.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;
        }
    });

But I just found that whichever way I chose, the edittext area could only be disabled from long clickable, which then prevents user from accessing the "select all, copy and paste" menu through long clicking. But both the 2 solution DID NOT prevent the user from accessing the "paste" function through just a simple tap on the cursor.

So my question is: how could I TOTALLY block user from copy and paste function in a certain Edittext. Is anyone help? Thx a lot


Solution

  • There is one possibility, by disabling the cursor handler. You won't get the paste button, but you will also not be able to move the cursor with touch.

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getActionMasked() == MotionEvent.ACTION_UP && mDisableCursorHandle) {
            // Hack to prevent keyboard and insertion handle from showing.
            cancelLongPress();
        }
        return super.onTouchEvent(event);
    }