[android] How to disable EditText in Android

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

This question is related to android android-edittext disabled-input

The answer is


try this Kotlin code,

editField.isEnabled = !editField.isEnabled


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


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


Edittext edittext = (EditText)findViewById(R.id.edit);
edittext.setEnabled(false);

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.


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


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.


To disable a edittext in android:

editText.setEnabled(false);

Assuming editText is you EditText object:

editText.setEnabled(false);

edittext.setFocusable(false); 
edittext.setEnabled(false);

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

imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
  //removes on screen keyboard

The below code disables the EditText in android

editText.setEnabled(false);

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


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"


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.


Right click the text area and untick the editable option:

enter image description here


In code:

editText.setEnabled(false);

Or, in XML:

android:editable="false"

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);
}

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-edittext

This view is not constrained vertically. At runtime it will jump to the left unless you add a vertical constraint Design Android EditText to show error message as described by google Change EditText hint color when using TextInputLayout How to change the floating label color of TextInputLayout The specified child already has a parent. You must call removeView() on the child's parent first (Android) Edittext change border color with shape.xml Changing EditText bottom line color with appcompat v7 Soft keyboard open and close listener in an activity in Android EditText underline below text property Custom designing EditText

Examples related to disabled-input

How to disable a input in angular2 Disable button after click in JQuery How to remove "disabled" attribute using jQuery? How to make html <select> element look like "disabled", but pass values? Disabled form fields not submitting data How to disable EditText in Android Disable/enable an input with jQuery? Values of disabled inputs will not be submitted