How to disable EditText in Android

142,667

Solution 1

I think its a bug in android..It can be fixed by adding this patch :)
Check these links question 1 and question 2

Hope it will be useful.

Solution 2

you can use EditText.setFocusable(false) to disable editing
EditText.setFocusableInTouchMode(true) to enable editing;

Solution 3

In code:

editText.setEnabled(false);

Or, in XML:

android:editable="false"

Solution 4

Assuming editText is you EditText object:

editText.setEnabled(false);

Solution 5

You can try the following method :

private void disableEditText(EditText editText) {
    editText.setFocusable(false);
    editText.setEnabled(false);
    editText.setCursorVisible(false);
    editText.setKeyListener(null);
    editText.setBackgroundColor(Color.TRANSPARENT); 
}

Enabled EditText :

Enabled EditText

Disabled EditText :

Disabled EditText

It works for me and hope it helps you.

Share:
142,667

Related videos on Youtube

aj10
Author by

aj10

Updated on May 03, 2021

Comments

  • aj10
    aj10 about 3 years

    How can I disable typing in an EditText field in Android?

    • Kartik Domadiya
      Kartik Domadiya about 13 years
      Disable Keyboard on input in EditText ?
    • aj10
      aj10 about 13 years
      There are two editText fields.First is for entering email.if the email is valid then the second editText field is to be enabled.but the problem is while typing an invalid email and clicking on the next button on the soft keyboard it goes to the second text field and can enter data eventhough i've made edittext.setEnabled(false).i am using android 2.3.1
  • Mxyk
    Mxyk over 12 years
    For those who'd rather see the code right here rather than use a hyperlink: editText.setEnabled(false);, in combination with editText.setFocusable(false); will achieve this.
  • jelies
    jelies over 11 years
    The same works with XML: android:enabled="false" and android:focusable="false". Thanks guys!
  • ramit girdhar
    ramit girdhar over 9 years
    just set focusable property of your edittext to "false" and you are done. <!-- begin snippet: js hide: false --> <!-- language: lang-html --> <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> <!-- end snippet -->
  • timgcarlson
    timgcarlson almost 9 years
    This was my issue. I assumed that after setFocusable(false) I could just flip setFocusable(true)... not the case. Setting setFocusableInTouchMode(true) did the trick for me.
  • Syed Raza Mehdi
    Syed Raza Mehdi almost 8 years
    good answer but it can be improved by removing the line in which the background color is being change and add editText.setKeyListener(null);
  • Igor Escodro
    Igor Escodro over 4 years
    Welcome to Stack Overflow, Nuwan Bandara. Your answer is related to JTextArea and not EditText, which is from Android Framework. It is unlikely that you are providing an answer for this question.
  • BabyishTank
    BabyishTank over 2 years
    this do nothing
  • Martin
    Martin over 2 years
    This is a great answer - it keeps the edit text looking the same