Reputation: 1127
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.
Upvotes: 5
Views: 4118
Reputation: 537
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);
Upvotes: 4
Reputation: 1127
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);
}
Upvotes: 0
Reputation: 148
EditText myEditText = (EditText) findViewById(R.id.my_edit_text);
myEditText.setTextIsSelectable(false);
Upvotes: 0
Reputation: 2267
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
}
};
Upvotes: 0
Reputation: 89
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.
Upvotes: 0