244

In my application, I have an EditText that the user only has Read access not Write access.

In code I set android:enabled="false".

Although the background of EditText changed to dark, when I click on it the keyboard pops up and I can change the text.

What should I set to disable EditText?

1
  • 3
    It's possible to preserve both the style of the view and the scrolling behaviour. To disable an EditText while keeping this properties, just use UI.setReadOnly(myEditText, true) from this library. If you want to replicate this behaviour without the library, check out the source code for this small method.
    – caw
    Mar 9, 2015 at 15:40

28 Answers 28

285

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

9
  • 1
    thanks Nailuj, unfortunately it didn't work. After adding your suggestion keyboard pops up and there is ability to change the text.
    – Hesam
    Nov 28, 2010 at 16:45
  • 22
    android:editable is deprecated. android:inputType="none" should be used instead but it contains bug and it does not work. It would be nice if we all star the issue here: code.google.com/p/android/issues/detail?id=2854 Mar 3, 2011 at 13:13
  • 97
    editText.setEnabled(false); editText.setInputType(InputType.TYPE_NULL);
    – Asymptote
    Jan 9, 2012 at 9:58
  • 33
    android:editable="false" android:focusable="false" works great for me (forget about deprecated, i think android:editable has nothing to do with android:inputType) May 29, 2013 at 8:32
  • 18
    For all those, for whom android:inputType="none" isn't working, try: android:inputType="none" with android:focusable="false" Oct 9, 2018 at 10:36
189

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

7
  • 1
    It works, but it's not a good practice because the view looks like it can be editable but it can't. Worked fine for me because my View had a custom background, so +1.
    – kaneda
    Apr 7, 2012 at 0:06
  • 5
    @kaneda. I actually prefer this. I don't want my box greyed out. :)
    – IAmGroot
    Jun 28, 2012 at 15:39
  • 2
    In Android 4.4, this options keeps the original text colour. Using editText.setEnabled(false); will change the text colour to grey. However, spinners does not change the text colour when using setEnabled, making it an inconsistent experience.
    – tbraun
    Sep 30, 2014 at 10:05
  • why not setFocusable(true) ? see this link for the difference between setFocusable() and setFocusableInTouchMode(): stackoverflow.com/questions/23799064/…
    – kenju
    Aug 26, 2015 at 5:24
  • This really fit my needs. I created a compound custom view with an EditText inside. I am having problem making the custom view of mine to just accept clicks but not editable as this launches a date picker dialog. This solution works as it doesn't disable onClickListeners
    – nww04
    Dec 19, 2016 at 2:46
78

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.

2
  • 1
    This looks like a TextView. It's so amazing. This is what I looking for. Apr 27, 2017 at 12:00
  • 9
    In case someone has the same problem with enabling back - you got to call editText.setFocusableInTouchMode(true) instead of editText.setFocusable(true).
    – Dart Dega
    Mar 1, 2018 at 8:30
72

Use this to disable user input

android:focusable="false"

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

1
  • 2
    This should be the accepted answer. Setting input type to 'none' as suggested in other answers does not (always) work. Setting enabled to 'false' prevents editing but also dims the text which may or may not be desired.
    – user3386180
    Sep 6, 2017 at 10:17
55

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

4
  • 1
    You can add android:longClickable="false" along with android:focusable="false" to remove the temporary highlight when touching this view. This also prevents the "select all" popup from appearing.
    – CzarMatt
    Jul 6, 2020 at 3:33
  • @CzarMatt instead you should use cursor visible to false
    – user14162811
    Apr 1, 2021 at 11:23
  • using enter of keyboard and imeoptions next from previous edittext you can still type there
    – user924
    May 7, 2021 at 11:39
  • How make the text selectable when android:focusable="false"? Nov 26, 2021 at 13:36
21

As android:editable="false" is depricated.You can use InputType TYPE_NULL on EditText

use like this :

editText.setInputType(InputType.TYPE_NULL);
3
  • 1
    Liked this answer because it does not disable the onClickListener on the field. Mar 29, 2017 at 10:21
  • 1
    what to do if the inputType is password? TYPE_NULL makes password visible. Mar 2, 2020 at 16:04
  • Thanks! It works together with android:focusable="false", while android:inputType="none" doesn't. In that case we can paste text via long click.
    – CoolMind
    Oct 31, 2023 at 14:42
21

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);
2
  • use android:enabled="true" for enable Mar 2, 2018 at 6:34
  • this disable the scroll and in my case I want to disable the editing but not the scroll
    – Alberto M
    Aug 18, 2021 at 10:41
9

Simply:

editText.setEnabled(false);
2
  • 1
    How does it differ from the previous answer (so as question)?
    – Onik
    Nov 7, 2015 at 13:11
  • setFocusable(false) also lets you set an OnClickListener. Nov 8, 2015 at 13:13
9

I use google newly released Material Design Library. In my case, it works when I use android:focusable="false" and android:cursorVisible="false"

<com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/to_time_input_layout"
                    app:endIconMode="custom"
                    app:endIconDrawable="@drawable/ic_clock"
                    app:endIconContentDescription="ToTime"
                    app:endIconTint="@color/colorAccent"
                    style="@style/OutlinedEditTextStyle"
                    android:hint="To Time">

                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/to_time_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusable="false"
                        android:cursorVisible="false" />

</com.google.android.material.textfield.TextInputLayout>
8

To disable the functionality of an EditText, just use:

EditText.setInputType(InputType.TYPE_NULL);

in case you want to enable it some way, then you can use:

EditText.setInputType(InputType.TYPE_CLASS_TEXT);
2
  • 1
    what to do if the inputType is password? TYPE_NULL makes password visible. Mar 2, 2020 at 16:03
  • yeah this breaks any kind of specific input (like in my case textMultiLine)
    – Alberto M
    Aug 18, 2021 at 10:44
6

Disable = FOCUS+CLICK+CURSOR

Disabling focus, click, and cursor visibility does the trick for me.

Here is the code in XML

<EditText
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:cursorVisible="false"
    android:clickable="false"
    />
5

Set below properties in class:

editText.setFocusable(false);
editText.setEnabled(false);

It will work smoothly as you required.

1
  • only enabled was sufficient
    – ateebahmed
    Dec 8, 2018 at 19:19
5

This will make your edittext disabled.

editText.setEnabled(false);

And by using this

editText.setInputType(InputType.TYPE_NULL);

Will just make your Edittext not show your softkeyboard, but if it is connected to a physical keyboard, it will let you type.

1
  • what to do if the inputType is password? TYPE_NULL makes password visible. Mar 2, 2020 at 16:03
4

if you use android:editable="false", eclipse will remind you this message "android:editable is deprecated: Use inputType instead".

So, I use android:focusable="false" instead, it worked well for me.

4
android:editable="false"

is now deprecated and use

 YourEditText.setInputType(InputType.TYPE_NULL);
3
  • what to do if the inputType is password? TYPE_NULL makes password visible. Mar 2, 2020 at 16:03
  • @ilyamuromets This answer might help stackoverflow.com/a/16720520/7704650 with TYPE_TEXT_VARIATION_PASSWORD Follow this for more developer.android.com/reference/android/text/InputType Mar 3, 2020 at 3:46
  • I want to disable input in the EditText while soft keyboard is opened and user trying to input text. InputType.TYPE_NULL works great with isFocusable and isFocusableInTouchMode in false value. But if inputType of the input field is textPassword then InputType.TYPE_NULL make password value visible. If I set InputType.TYPE_NULL | InputType.TYPE_TEXT_VARIATION_PASSWORD then password also becomes visible and user might input text. Mar 4, 2020 at 8:13
4

Using android:editable="false" is Depracted. Instead you'll need to Use android:focusable="false"

1
  • 1
    using enter of keyboard and imeoptions next from previous edittext you can still type there
    – user924
    May 7, 2021 at 11:39
3

Use TextView instead.

1

In my case I needed my EditText to scroll text if no. of lines exceed maxLines when its disabled. This implementation worked perfectly for me.

private void setIsChatEditTextEditable(boolean value)
{
    if(value)
    {
        mEdittext.setCursorVisible(true);
        mEdittext.setSelection(chat_edittext.length());
       // use new EditText(getApplicationContext()).getKeyListener()) if required below
        mEdittext.setKeyListener(new AppCompatEditText(getApplicationContext()).getKeyListener());  
    }
    else
    {
        mEdittext.setCursorVisible(false);
        mEdittext.setKeyListener(null);
    }
}
1

Try this one, works fine for me:

public class CustomEdittext extends EditText {

Boolean mIsTextEditor=true;
public CustomEdittext(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public boolean onCheckIsTextEditor() {
    // TODO Auto-generated method stub
    return mIsTextEditor;
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    mIsTextEditor=false;
    Boolean mOnTouchEvent=super.onTouchEvent(event);
    mIsTextEditor=true;     
    return mOnTouchEvent;
} }

Note: You need to add this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); on your activity or else keyboard will popup at first time.

1

As some answer mention it, if you disable the editText he become gray and if you set focusable false the cursor is displaying.

If you would like to do it only with xml this did the trick

       <YourFloatLabel
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <EditText
                android:id="@+id/view_ads_search_select"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:focusable="true"
                android:clickable="true"/>  
       </YourFloatLabel>

I simply add a FrameLayout appear above the editText and set it focusable and clickable so the editText can't be click.

0

This works for me:

android:focusable="false"

1
  • 2
    How is this different than what Sampath Kumar posted 4 years ago?
    – o-90
    Jan 27, 2017 at 21:00
0

From @Asymptote's comment on the accepted answer, use:

myEditText.setEnabled(false);
myEditText.setInputType(InputType.TYPE_NULL);

...and Bob's your uncle.

0

Today I still use editable="false", but also with focusable="false".

I think the case we need to make an EditText un-editable, is because we want to keep its EditText style (with that underline, with hint, etc), but it accepts other inputs instead of text. For example a dropdown list.

In such use case, we need to have the EditText clickable (thus enabled="false" is not suitable). Setting focusable="false" do this trick, however, I can still long hold on the EditText and paste my own text onto it from clipboard. Depending on your code and handling this can even crash your app.

So I also used editable="false" and now everything is great, except the warning.

0

you can use android:focusable="false" but also need to disable cursor otherwise copy/paste function would still work.

so, use

android:focusable="false"
android:cursorVisible="false"
0

There are multiple was how to achieve multiple levels of disabled.

  • editText.setShowSoftInputOnFocus(false); and editText.setFocusable

prevents the EditText from showing keyboard - writing in some text. But cursor is still visible and user can paste in some text.

  • editText.setCursorVisible(false)

hides the cursor. Not sure why would you want to do that tho. User can input text & paste.

  • editText.setKeyListener(null)

I find this way most convenient. There is no way how user can input text, but widget still works with OnClickListener if you want to trigger action when user touches it

  • editText.setEnabled(false);

completely disables EditText. It is literally 'read-only', user cannot input any text in it and (for example) OnClickListener doesn't work with it.

TextEdit documentation

0

You can use this ext function in Kotlin:

fun EditText.disableInput() {
    isFocusable = false
    isEnabled = false
    isCursorVisible = false
    keyListener = null
    setBackgroundColor(Color.TRANSPARENT)
}

like so:

myEditText.disableInput()
0

The answers above don't really work in all cases, e.g. the Talkback mode.

I added an additional view on top of the EditText and assigned a click listener to it.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    ...
            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/textInputEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

            </com.google.android.material.textfield.TextInputLayout>

            <View
                android:id="@+id/btnTextInputLayout"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="?selectableItemBackground"
                app:layout_constraintBottom_toBottomOf="@+id/textInputLayout"
                app:layout_constraintEnd_toEndOf="@+id/textInputLayout"
                app:layout_constraintStart_toStartOf="@+id/textInputLayout"
                app:layout_constraintTop_toTopOf="@+id/textInputLayout" />

    ...

</androidx.constraintlayout.widget.ConstraintLayout>

Helper functions:

    private fun makeOverlayEditTextEditable(
        editText: EditText,
        clickOverlayView: View,
        inputType: Int
    ) {
        enableEditTextInput(editText, inputType)
        clickOverlayView.isVisible = false
    }

    private fun makeOverlayEditTextClickable(
        editText: EditText,
        clickOverlayView: View,
        clickListener: () -> Unit
    ) {
        disableEditTextInput(editText)
        clickOverlayView.setOnClickListener {
            clickListener()
        }
        clickOverlayView.isVisible = true
    }
    @SuppressLint("ClickableViewAccessibility")
    private fun disableEditTextInput(editText: EditText) {
        editText.inputType = InputType.TYPE_NULL
        editText.isFocusable = false
        editText.isFocusableInTouchMode = false
        editText.isClickable = false
        editText.setOnTouchListener { _, _ -> true }
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun enableEditTextInput(editText: EditText, inputType: Int) {
        editText.inputType = inputType
        editText.isFocusable = true
        editText.isFocusableInTouchMode = true
        editText.isClickable = true
        editText.setOnTouchListener(null)
    }

Usage:

// Make EditText editable
makeOverlayEditTextEditable(
    editText = binding.textInputLayout,
    clickOverlayView = binding.btnTextInputLayout,
    inputType = InputType.TYPE_CLASS_TEXT
)

// Make EditText clickable
makeOverlayEditTextClickable(
    editText = binding.textInputLayout,
    clickOverlayView = binding.btnTextInputLayout,
) {
    // Handle click
}

If this pattern is used all across the application, it makes sense to abstract this logic in the custom view.

-1

Set this in your XML code, It works.

android:focusableInTouchMode="false"

Not the answer you're looking for? Browse other questions tagged or ask your own question.