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 editingEditText.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 :
Disabled EditText :
It works for me and hope it helps you.
Related videos on Youtube
Author by
aj10
Updated on May 03, 2021Comments
-
aj10 about 3 years
How can I disable typing in an
EditText
field in Android?-
Kartik Domadiya about 13 yearsDisable Keyboard on input in EditText ?
-
aj10 about 13 yearsThere 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 over 12 yearsFor those who'd rather see the code right here rather than use a hyperlink:
editText.setEnabled(false);
, in combination witheditText.setFocusable(false);
will achieve this. -
jelies over 11 yearsThe same works with XML:
android:enabled="false"
andandroid:focusable="false"
. Thanks guys! -
ramit girdhar over 9 yearsjust 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 almost 9 yearsThis was my issue. I assumed that after
setFocusable(false)
I could just flip setFocusable(true)... not the case. SettingsetFocusableInTouchMode(true)
did the trick for me. -
Syed Raza Mehdi almost 8 yearsgood 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 over 4 yearsWelcome 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 over 2 yearsthis do nothing
-
Martin over 2 yearsThis is a great answer - it keeps the edit text looking the same