In my application, I have an EditText that the user only has Read access not Write access.
In code I set android:enabled="false"
.
Although the background of EditText changed to dark, when I click on it the keyboard pops up and I can change the text.
What should I set to disable EditText?
I believe the correct would be to set
android:editable="false"
.And if you wonder why my link point to the attributes of
TextView
, you the answer is becauseEditText
inherits fromTextView
:Update:
As mentioned in the comments below,
editable
is deprecated (since API level 3). You should instead be usinginputType
(with the valuenone
).As
android:editable="false"
deprecated Inxml
If you want in
java class
you can also use this programmaticallyThere are multiple was how to achieve multiple levels of disabled.
editText.setShowSoftInputOnFocus(false);
andeditText.setFocusable
prevents the
EditText
from showing keyboard - writing in some text. But cursor is still visible and user can paste in some text.editText.setCursorVisible(false)
hides the cursor. Not sure why would you want to do that tho. User can input text & paste.
editText.setKeyListener(null)
I find this way most convenient. There is no way how user can input text, but widget still works with
OnClickListener
if you want to trigger action when user touches iteditText.setEnabled(false);
completely disables
EditText
. It is literally 'read-only', user cannot input any text in it and (for example)OnClickListener
doesn't work with it.TextEdit documentation
In my case I needed my
EditText
to scroll text if no. of lines exceedmaxLines
when its disabled. This implementation worked perfectly for me.As some answer mention it, if you disable the editText he become gray and if you set focusable false the cursor is displaying.
If you would like to do it only with xml this did the trick
I simply add a FrameLayout appear above the editText and set it focusable and clickable so the editText can't be click.