Search code examples
androidandroid-edittext

How to make EditText not editable through XML in Android?


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.

Solution

  • 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());