92

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

0xCursor
  • 2,242
  • 4
  • 15
  • 33
aj10
  • 1,043
  • 1
  • 8
  • 11
  • 1
    Disable Keyboard on input in EditText ? – Kartik Domadiya May 04 '11 at 06:13
  • 1
    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 – aj10 May 04 '11 at 06:46

18 Answers18

72

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

Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
chaitanya
  • 1,726
  • 2
  • 17
  • 13
  • 6
    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. – timgcarlson Jul 17 '15 at 22:55
49

In code:

editText.setEnabled(false);

Kotlin:

editText.isEnabled = false

Or, in XML:

android:enabled="false"
xSynergyx
  • 85
  • 1
  • 9
Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
43

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.

ann
  • 576
  • 1
  • 10
  • 19
Midhun
  • 469
  • 4
  • 4
  • 119
    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. – Mxyk Jan 09 '12 at 15:00
  • 9
    The same works with XML: `android:enabled="false"` and `android:focusable="false"`. Thanks guys! – jelies Jan 21 '13 at 19:45
15

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.

Maksym Kalin
  • 1,693
  • 16
  • 18
  • good answer but it can be improved by removing the line in which the background color is being change and add editText.setKeyListener(null); – Syed Raza Mehdi Aug 30 '16 at 09:45
14

Assuming editText is you EditText object:

editText.setEnabled(false);
MByD
  • 135,866
  • 28
  • 264
  • 277
12

Just set focusable property of your edittext to "false" and you are done.

<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>

Below options doesn't work

In code:

editText.setEnabled(false); Or, in XML: android:editable="false"

ramit girdhar
  • 2,272
  • 1
  • 25
  • 26
7
Edittext edittext = (EditText)findViewById(R.id.edit);
edittext.setEnabled(false);
saigopi.me
  • 14,011
  • 2
  • 83
  • 54
6

Enable:

private void enableEditText() {
    mEditText.setFocusableInTouchMode(true);
    mEditText.setFocusable(true);
    mEditText.setEnabled(true);
}

Disable:

private void disableEditText() {
    mEditText.setEnabled(false);
    mEditText.setFocusable(false);
    mEditText.setFocusableInTouchMode(false);
}
Danylo Volokh
  • 4,149
  • 2
  • 33
  • 40
3

You can write edittext.setInputType(0) if you want to show the edittext but not input any values

GSerg
  • 76,472
  • 17
  • 159
  • 346
Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30
2

To disable a edittext in android:

editText.setEnabled(false);
GSerg
  • 76,472
  • 17
  • 159
  • 346
Vineet
  • 99
  • 2
2

The below code disables the EditText in android

editText.setEnabled(false);
GSerg
  • 76,472
  • 17
  • 159
  • 346
uday
  • 1,625
  • 3
  • 14
  • 16
2
edittext.setFocusable(false); 
edittext.setEnabled(false);

InputMethodManager imm = (InputMethodManager)
  getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
  //removes on screen keyboard
dldnh
  • 8,923
  • 3
  • 40
  • 52
Kumar Sukhani
  • 377
  • 2
  • 8
1

According to me...if you have already declared the edittext in the XML file and set the property in the XML file "android:enabled=false" and disable at runtime to it at that time in java file adding only 2 or 3 lines

  1. First create the object
  2. Declare EditText et1;
  3. Get by id

    et1 = (EditText) FindViewById(R.id.edittext1);
    et1.setEnabled(false);

You can do enable or disable to every control at run time by java file and design time by XML file.

ann
  • 576
  • 1
  • 10
  • 19
1

The XML editable property is deprecated since API LEVEL 15.

You should use now the inputType property with the value none. But there is a bug that makes this functionality useless.

Here you can follow the issue status.

Brugui
  • 574
  • 6
  • 20
0

Write this in parent:

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

Example:

<RelativeLayout 
android:id="@+id/menu_2_zawezanie_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/menu_2_horizontalScrollView"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:background="@drawable/rame">

<EditText
    android:id="@+id/menu2_et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/menu2_ibtn"
    android:gravity="center"
    android:hint="@string/filtruj" >
</EditText>
<ImageButton
    android:id="@+id/menu2_ibtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="@null"
    android:src="@drawable/selector_btn" />

Gimer
  • 13
  • 5
0

Set inputType attribute to none in your layout.xml file under EditText

Evgeniy Mishustin
  • 3,343
  • 3
  • 42
  • 81
0

try this Kotlin code,

editField.isEnabled = !editField.isEnabled

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
-2

Right click the text area and untick the editable option:

enter image description here

Das_Geek
  • 2,775
  • 7
  • 20
  • 26
  • 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. – Igor Escodro Dec 18 '19 at 17:06