How to make EditText not editable through XML in Android?


Question

Can anyone tell me how to make an EditText not editable via XML? I tried setting android:editable to false, but

  1. it is deprecated; and
  2. it didn't work.
1
288
2/12/2016 11:47:38 PM

Accepted Answer

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());
391
2/25/2016 12:25:54 PM

Add this to your EditText xml file:

<EditText ...
        android:clickable="false" 
        android:cursorVisible="false" 
        android:focusable="false" 
        android:focusableInTouchMode="false">
</EditText>

It will do the same effect as android:editable="false". Worked for me, hope it'll work for you too.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon