Can anyone tell me how to make an EditText
not editable via XML? I tried setting android:editable
to false
, but
Use this simple code:
textView.setKeyListener(null);
It works.
Edit : To add KeyListener
later, do following
1 : set key listener to tag of textView
textView.setTag(textView.getKeyListener());
textView.setKeyListener(null);
2 : get key listener from tag and set it back to textView
textView.setKeyListener((KeyListener) textView.getTag());
They made "editable" deprecated but didn't provide a working corresponding one in inputType.
By the way, does inputType="none" has any effect? Using it or not does not make any difference as far as I see.
For example, the default editText is said to be single line. But you have to select an inputType for it to be single line. And if you select "none", it is still multiline.
Just use android:enabled="false"
. This will also give an appearance to the EditText
which will make it look not editable.
In addition to @mahe madi you can try this as well
editText.setEnabled(false);
editor.setTextColor(Color.BLACK);
This method will hide cursor, lose focus and more importantly set text color to black behaving like TextView.
And to revert Back to editText simply do this to gain all the properties of EditText.
editText.setEnabled(true);
Hope it Helps :)
if you want to click it, but not want to edit it, try:
android:focusable="false"
If you want to do it in java code just use this line to disable it:
editText.setEnabled(false);
And this to enable it:
editText.setEnabled(true);