Android - EditText not editable and not selectable
Solution 1
Sorry guys, at least for my setup (Sdk Version 25 with AppCompatActivity)
editText.setFocusable(false);
will still let me push the return button of the virtual keyboard, adding lines to the text. If you add
editText.setEnabled(false);
this behaviour stopps, however, the text is greyed out.
Therefore, I think the following would be a better solution:
editText.setInputType(InputType.TYPE_NULL);
Solution 2
This works fine; just set focusable property of your edittext to "false" and you are done.
<EditText
android:id="@+id/EditTextInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:gravity="right"
android:cursorVisible="true">
</EditText>
Solution 3
I would implement the following code:
editText.setFocusable(false);
editText.setClickable(false);
editText.setEnabled(false);
Which should prevent interacting with the EditText
. Read more about the View
class here since EditText
extends from it:
https://developer.android.com/reference/android/view/View.html#setFocusable(boolean)
Vin
Updated on June 04, 2022Comments
-
Vin almost 2 years
In my activity code I have these lines
EditText editText=(EditText)findViewById(R.id.editText); editText.setTextIsSelectable(false);
What I want to achieve is a
EditText
uneditable and unselectable. With the previous line I try to achieve the second option, but it'doesn't work. TheEditText
is still selectable.In order to make the
EditText
uneditable I have not found any method such assetEditable
or similar. I read about settingInputType
but I had no success.However the unselectable constraint can be weakened. My main goal is that the text inside the
EditText
cannot change for any reason. For these reason theEditRext
could be selectable but I want that no user can cut the text.I want no XML but only Java code. All these things are needed to to programmatically.
-
Vin over 6 years
setEnabled(false)
disable theEditText
. It's a drastic solution. -
Vin over 6 yearsIt doesn't matter.
setFocusable(false)
is the way. -
Vin over 6 years
setClickable(false)
seems to do no difference.setFocusable(false)
is the right way. -
gi097 over 6 years@Vin only using
setFocusable()
does not disable it, a user can still paste data to theEditText
. -
Vin over 6 yearsI didn't think it. Thank you.
-
Vin over 6 yearsWait wait wait.
setFocusable(false)
is not enough. Because text can be pasted into theEditText
, and this is not a good thing. -
Vin over 6 yearsInstead to use
setEnabled(false)
. I usedsetFocusable(false)
,setClickable(false)
andsetLongClickable(false)
. This seems to prevent text pasting inside theEditText
-
Vin over 6 years@KalaBalik It doesn't matter means that the solution is understandable. In this case
android:focusable="false"
is exactlysetFocusable(false)
. Sometimes XML is not fully translatable to Java code, in that case I will not accepted the question. -
Vin over 6 yearsThe magic number
0
in thesetInputType()
method, what does it mean? -
kalabalik over 6 yearsThe docs say: >"There is no content type. The text is not editable."
-
Vin over 6 yearsI tried. The only input type seems to make the
EditText
not clickable and not long-clickable. I don't know ifsetClickable(false)
andsetLongClickable(false)
are unnecessary now. -
kalabalik over 6 yearsActually, I can't reproduce what @Vin said earlier about the click and the long click. If no one intervenes soon, I will take out lines 2 and 3 from my answer. Lines 1 and 4 suffice.
-
Vin over 6 yearsIt seems that with only line 4 (
setInputType(0)
) theEditText
is not editable and not selectable. -
Vin over 6 years
setFocusable(false)
is only if anyone would that allEditText
s are not highlited when are clicked. -
lucidbrot about 5 yearsPerfect for making an EditText that is actually not editable. This answer also avoids the keyboard showing.