0

I need to de-activate all copy/paste operations from my EditText. The setLongClickable(false) alone is not working. Please Help.

srjhnd
  • 189
  • 1
  • 10
MLSE
  • 31
  • 7

3 Answers3

0

You can try setting it via ActionMode as well, like so:

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);
aliaksei
  • 714
  • 1
  • 9
  • 23
0

Along with setLongClicable(false), add the below code

mEditText.setCustomSelectionActionModeCallback(new 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;
        }
    });

If you need this functionality at multiple places, You can create an extension with above code and call it on edittext

Viswanath Kumar Sandu
  • 2,230
  • 2
  • 17
  • 34
0

So, I found an non complete answer to my question, I will be following those steps :

1 - extend a custom class from EditText or TextInputEditText

`

open class CustomEditText : TextInputEditText {
    constructor(context: Context?) : super(context) {
        disableCopyPastMenu()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        disableCopyPastMenu()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        disableCopyPastMenu()
    }
}

` 2- in your layout use replace your edittext with customEditText

`<xxxx.CustomEditText
       android:id="xxxxxx"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="xxx"
       android:inputType="xx" />`

3- add a method to disableMenu() where we can find copy and past

`private fun disableCopyPastMenu() { this.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) {}
    }
    this.isLongClickable = false
    this.isClickable = false
    this.setOnTouchListener { v, event ->
        clearFocus()
        return@setOnTouchListener false
    }
}`

4 - override onTouchEvent() with a method to disable insertion

`

 override fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_DOWN) {
            disableInsertion()
        } return super.onTouchEvent(event)
    }

    private fun disableInsertion() {
        try {
            val editorField = TextView::class.java.getDeclaredField("))
                    editorField . isAccessible = true
                    val editorObject = editorField [this]
            val editorClass = Class.forName(")
                    val insertionControllerEnabled = editorClass . getDeclaredField (")
                    insertionControllerEnabled . isAccessible = true
            insertionControllerEnabled[editorObject] = false
        } catch (exception: Exception) { // add exception if needed
        }
    }

` 5- override isSuggedtionsEnabled() making it return false

`

override fun isSuggestionsEnabled(): Boolean {
        return false
    }

`

this code works well, except sometimes the copy past menu is showing i don't know why!!!

also I'd like to unit tests this CustumEditText, any guess's to start with.

MLSE
  • 31
  • 7