Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling of EditText in Android

People also ask

What is the use of EditText in android?

A EditText is an overlay over TextView that configures itself to be editable. It is the predefined subclass of TextView that includes rich editing capabilities.

Is EditText deprecated?

Android Studio:EditText editable is deprecated How to use inputType.


I believe the correct would be to set android:editable="false".

And if you wonder why my link point to the attributes of TextView, you the answer is because EditText inherits from TextView:

EditText is a thin veneer over TextView that configures itself to be editable.

Update:
As mentioned in the comments below, editable is deprecated (since API level 3). You should instead be using inputType (with the value none).


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


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.


Use this to disable user input

android:focusable="false"

android:editable="false" This method is deprecated one.


For disable edit EditText, I think we can use focusable OR enable but

  1. Using android:enabled=... or editText.setEnabled(...)

    It also changes the text color in EditText to gray.
    When clicked it have no effect

  2. Using android:focusable=... or editText.setFocusable(false) - editText.setFocusableInTouchMode(true)

    It doesn't change text color of EditText
    When clicked it highlights the EditText bottom line for about few millisecond

Output

enter image description here


As android:editable="false" deprecated In xml

Use android:enabled="false" it's simple. Why use more code?

If you want in java class you can also use this programmatically

 editText.setEnabled(false);