241

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?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Hesam
  • 52,260
  • 74
  • 224
  • 365
  • 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](https://github.com/delight-im/Android-BaseLib). If you want to replicate this behaviour without the library, check out [the source code](https://github.com/delight-im/Android-BaseLib/blob/62299c79d100e38627600907e755d563de072234/Source/src/im/delight/android/baselib/UI.java#L264) for this small method. – caw Mar 09 '15 at 15:40

28 Answers28

282

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

Julian
  • 20,008
  • 17
  • 77
  • 108
  • 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 '10 at 16:45
  • @Hesam: I suspect you're doing something wrong then. If you read the documentation for the `android:editable` attribute, it says that the `TextView` (and `EditText` which inherits from the `TextView`) will not have an input method if this attribute is set to `false`. I also just tested this myself, and setting `android:editable="false"` on an `EditText` in my own application makes it impossible to write text in it (it is still possible to position the cursor inside it though). You should post an example from your code, then we might see what you're doing wrong. – Julian Nov 28 '10 at 16:51
  • thanks Nailuj, Yes you told correct. I put your code in a wrong place. however, after I correct it, it works fine. – Hesam Nov 28 '10 at 16:57
  • 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: http://code.google.com/p/android/issues/detail?id=2854 – Viktor Brešan Mar 03 '11 at 13:13
  • 96
    `editText.setEnabled(false); editText.setInputType(InputType.TYPE_NULL);` – Asymptote Jan 09 '12 at 09:58
  • 1
    @ViktorBrešan also when I have multiline EditText and I set android:inputType="none" it changes it to single line. It's a heck to disable a multiline EditText yet let the user read the entire text. – Adil Malik Feb 05 '13 at 10:46
  • 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) – Muhammad Babar May 29 '13 at 08:32
  • The issue for me is that the wrapping breaks when I do this. EditText doesn't seem to wrap correctly. – fobbymaster May 12 '17 at 23:05
  • 16
    For all those, for whom android:inputType="none" isn't working, try: android:inputType="none" with android:focusable="false" – Surendra Kumar Oct 09 '18 at 10:36
185

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

favo
  • 5,426
  • 9
  • 42
  • 61
Sean Android
  • 1,867
  • 1
  • 11
  • 2
  • 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 07 '12 at 00:06
  • 5
    @kaneda. I actually prefer this. I don't want my box greyed out. :) – IAmGroot Jun 28 '12 at 15:39
  • 1
    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 '14 at 10:05
  • why not `setFocusable(true)` ? see this link for the difference between `setFocusable()` and `setFocusableInTouchMode()`: http://stackoverflow.com/questions/23799064/what-is-the-difference-between-setfocusable-and-setfocusableintouchmode – kenju Aug 26 '15 at 05: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 – Neon Warge Dec 19 '16 at 02:46
  • but you still can make long tab on EditText view and paste text from clipboard – user924 May 08 '20 at 06:23
  • to prevent long touch you can also add 'android:longClickable="false"' as specified in a comment on an answer below. – Joseph Sang Mar 24 '21 at 07:30
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.

Maksym Kalin
  • 1,693
  • 16
  • 18
  • 1
    This looks like a TextView. It's so amazing. This is what I looking for. – Yunus Haznedar Apr 27 '17 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 01 '18 at 08:30
72

Use this to disable user input

android:focusable="false"

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

Sampath Kumar
  • 4,433
  • 2
  • 27
  • 42
  • 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. –  Sep 06 '17 at 10:17
54

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

Community
  • 1
  • 1
Linh
  • 57,942
  • 23
  • 262
  • 279
  • 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 06 '20 at 03:33
  • @CzarMatt instead you should use cursor visible to false –  Apr 01 '21 at 11:23
  • using enter of keyboard and imeoptions next from previous edittext you can still type there – user924 May 07 '21 at 11:39
  • How make the text selectable when android:focusable="false"? – Mark Delphi Nov 26 '21 at 13:36
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);
android_jain
  • 788
  • 8
  • 19
20

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

use like this :

editText.setInputType(InputType.TYPE_NULL);
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
9

Simply:

editText.setEnabled(false);
Oded Breiner
  • 28,523
  • 10
  • 105
  • 71
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>
Aminul Haque Aome
  • 2,261
  • 21
  • 34
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);
Suri
  • 693
  • 7
  • 16
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"
    />
Community
  • 1
  • 1
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
5

Set below properties in class:

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

It will work smoothly as you required.

stkent
  • 19,772
  • 14
  • 85
  • 111
Rank
  • 900
  • 9
  • 17
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.

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.

wangzhengyi
  • 856
  • 8
  • 8
4
android:editable="false"

is now deprecated and use

 YourEditText.setInputType(InputType.TYPE_NULL);
emkarachchi
  • 750
  • 1
  • 7
  • 18
  • what to do if the inputType is password? TYPE_NULL makes password visible. – ilyamuromets Mar 02 '20 at 16:03
  • @ilyamuromets This answer might help https://stackoverflow.com/a/16720520/7704650 with `TYPE_TEXT_VARIATION_PASSWORD` Follow this for more https://developer.android.com/reference/android/text/InputType – emkarachchi Mar 03 '20 at 03: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. – ilyamuromets Mar 04 '20 at 08:13
4

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

3

Use TextView instead.

cx0der
  • 432
  • 5
  • 20
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.

Favas Kv
  • 2,961
  • 2
  • 28
  • 38
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);
    }
}
Rishabh876
  • 3,010
  • 2
  • 20
  • 37
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.

Vodet
  • 1,491
  • 1
  • 18
  • 36
0

This works for me:

android:focusable="false"

Annie
  • 15
  • 4
0

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

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

...and Bob's your uncle.

Tom Howard
  • 4,672
  • 2
  • 43
  • 48
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.

Sira Lam
  • 5,179
  • 3
  • 34
  • 68
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"
M.Usman
  • 2,049
  • 22
  • 25
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

DeepBlue
  • 591
  • 9
  • 18
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()
Choletski
  • 7,074
  • 6
  • 43
  • 64
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.

Veniamin
  • 774
  • 5
  • 12
-1

Set this in your XML code, It works.

android:focusableInTouchMode="false"

Sujeet
  • 558
  • 8
  • 13