Android EditText - Editable but not Selectable?

3.9k Views Asked by At

Is it possible to make an EditText editable but not selectable? I want to be able to edit a bit of text, then drag it around the page. It works but while you're dragging it, that counts as a long click, so it selects some of the text.

Will I have to do something like make another view appear when I click on the EditText, have that capture the touch events instead, then hide it when the event action is up and return focus to the editText? That seems hacky/overly complicated.

5

There are 5 best solutions below

0
Andrew Torr On BEST ANSWER

I found a slightly less hacky solution than the one I mentioned above, and then tried (and it didn't work), but this does work:

first do this,

image_text.cancelLongPress();

which apparently cancels any inherited click, not just the long click, i.e. it shows the cursor, and it doesn't select any text, but it also doesn't bring up the keyboard, but that's easy:

View v = activity.getWindow().getCurrentFocus();
        if (v != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(v, 0);
        }
1
Vladimir Jovanović On

This should work:android:textIsSelectable="false"

2nd possible solution would be to put empty onLongClickListener that would consume event.

OnLongClickListener longClickListener = new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {\
        return true; //true = event consumed, false = event not consumed
    }
};
0
MrSalmon On

Maybe not exactly the solution you are looking for, but when the EditText is dropped into place, you could ensure there is no selection by calling editText.clearFocus();

This would be an issue if you needed to retain selection that was existing prior to dragging, but not if you just wanted to ensure the selection was cleared.

4
eggdeng On
EditText myEditText = (EditText) findViewById(R.id.my_edit_text);
myEditText.setTextIsSelectable(false);
0
YetAnotherUser On

Achieved desired behavior using setTextIsSelectable(false). The trick was to run setTextIsSelectable(true) first. It overcomes the bug/feature in the SDK (debug this method if you interested in). Tested on Android 7.1.1.

editText.setTextIsSelectable(true); // needed in order to next line work properly
editText.setTextIsSelectable(false);

// restore soft keyboard functionality broken by previous line
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
editText.setClickable(true);