84

I'm trying to make an EditText non editable with this code:

<EditText android:id="@+id/outputResult"
          android:inputType="text"
          android:editable="false"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/result" />

I had to add following line to make it non-editable

android:focusable="false" 

Am I doing something wrong?

pcCC28
  • 179
  • 1
  • 7
  • 20
Jjreina
  • 2,633
  • 6
  • 33
  • 54
  • know why does not work only with android:editable="false". Thanks. – Jjreina Feb 27 '12 at 18:32
  • @Zortkun: That is exactly what the OP is asking. – Squonk Feb 27 '12 at 18:44
  • 1
    @Jjreina: My guess is that it's because you're specifying an `inputType` and that may be overriding the `android:editable="false"` setting. I may be wrong though. – Squonk Feb 27 '12 at 18:46
  • MisterSquonk i remove inputType and work fine but show warnig This text field does not specify an inputType or a hint Thanks you very much. Now I had add android:inputType="none" and all ok, Thanks all – Jjreina Feb 27 '12 at 18:53

18 Answers18

200

android:editable="false" should work, but it is deprecated, you should be using android:inputType="none" instead.

Alternatively, if you want to do it in the code you could do this :

EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setEnabled(false);

This is also a viable alternative :

EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setKeyListener(null);

If you're going to make your EditText non-editable, may I suggest using the TextView widget instead of the EditText, since using a EditText seems kind of pointless in that case.

EDIT: Altered some information since I've found that android:editable is deprecated, and you should use android:inputType="none", but there is a bug about it on android code; So please check this.

Community
  • 1
  • 1
Jean-Philippe Roy
  • 4,752
  • 3
  • 27
  • 41
34

I guess I am late but use following together to make it work:

 android:inputType="none"
 android:enabled="false"
AkshayT
  • 2,901
  • 2
  • 28
  • 32
  • 5
    This should be the accepted answer. Using `android:inputType="none"` won't be what you are expecting unless you add `android:enabled="false"` – Hissaan Ali Nov 02 '19 at 08:50
  • This. Setting just either inputType="none" or enabled="false" doesn't seem to do anything.. must be both! – Bruce Feb 27 '22 at 04:17
18

In case you want to make an EditText not editable from code:

void disableInput(EditText editText){
    editText.setInputType(InputType.TYPE_NULL);
    editText.setTextIsSelectable(false);
    editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v,int keyCode,KeyEvent event) {
                return true;  // Blocks input from hardware keyboards.
        }
    });
}

In case you want to remove the horizontal line of your EditText, just add:

editText.setBackground(null);

If you want to let users copy the content of the EditText then use:

editText.setTextIsSelectable(true);
vovahost
  • 34,185
  • 17
  • 113
  • 116
11
 <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editTexteventdate"
            android:background="@null"
            android:hint="Date of Event"
            android:textSize="18dp"
            android:textColor="#fff"
            android:editable="false"
            android:focusable="false"
            android:clickable="false"

            />

Add this

android:editable="false"

android:focusable="false"

android:clickable="false"

its work for me

Dharmendra Mishra
  • 2,427
  • 1
  • 13
  • 9
  • Since editable is deprecated use this instead: ` android:inputType="none"`(along with the other ones you have). – BlueBoy Sep 05 '17 at 02:01
  • 1
    Not because it works for you it should work for us as well. You posted this on May 19th of this year so you must be aware you provided a deprecated answer? C'mon... – Neon Warge Nov 01 '17 at 01:41
  • 1
    User is still able to use `paste` option to change text of the `EditText`. – Muhammad Saqib Oct 23 '19 at 14:34
8

well android:editable="false" is deprecated and doesn't work anymore and android:inputType="none" would not give you the results you want

you could use

<EditText
            android:cursorVisible="false"
            android:focusableInTouchMode="false"
            android:maxLength="10"/>

and if you want to make the EditText editable again you could do that with code

//enable view's focus event on touch mode
edittext.setFocusableInTouchMode(true);

//enable cursor is visible
edittext.setCursorVisible(true);

// You have to focus on the edit text to avoid having to click it twice
//request focus 
edittext.requestFocus();

//show keypad is used to allow the user input text on the first click
InputMethodManager openKeyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 

openKeyboard.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);

Note I'm using android:focusableInTouchMode="false" and not

android:focusable="false" 

because when I use

edittext.setFocusable(true); 

I still have to add

edittext.setFocusableInTouchMode(true); 

Like this

edittext.setFocusableInTouchMode(true);
edittext.setFocusable(true);

for the edittext focus to be enabled again

Olayiwola Osho
  • 121
  • 2
  • 4
7

If you really want to use an editText and not a textView, then consider using these three:

android:focusable="false"

android:clickable="false"

android:longClickable="false"

EDIT: The "longClickable" attribute disables the long press actions that cause the "Paste" and or "Select All" options to show up as a tooltip.

Mofe Ejegi
  • 733
  • 11
  • 20
3

Add below line in youe xml edittext.

android:editable="false"
android:focusable="false"
android:clickable="false"

Its working for me

Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
3

Use This:

EditText edtText = (EditText) findViewById(R.id.yourEditTextId);
edtText.setEnabled(false);

and you can use android:enabled property in your xml file as well.

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
Gourav Samre
  • 134
  • 1
  • 7
2
android:enabled = false

Include that line in your layout xml file.

2

If you want your disabled EditText to look the same as your enabled one, you should use android:enabled="false" in combination with android:textColor="...".

Here is an example:

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

    <com.google.android.material.textfield.TextInputEditText
        ...
        android:text="..."
        android:enabled="false"
        android:textColor="@color/dark_grey"/>

</com.google.android.material.textfield.TextInputLayout>
Hristo Eftimov
  • 13,845
  • 13
  • 50
  • 77
Andre Thiele
  • 3,202
  • 3
  • 20
  • 43
2

If you want your edit text clickable but not editable, You can try this:

android:cursorVisible="false"
android:inputType="none"
android:clickable="true"
android:focusable="false"
iknow
  • 8,358
  • 12
  • 41
  • 68
Ram Chhabra
  • 421
  • 8
  • 11
1

I had this problem, too.

I did this:

<EditText
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:id="@+id/yourid"
    android:editable="false"
    android:inputType="none" />
Spoody
  • 2,852
  • 1
  • 26
  • 36
Cogiat84
  • 11
  • 2
1
android:inputType="none"
android:enabled="false"
android:clickable="false"

The clickable attribute is optional! You can still set text to the edit text programmatically!

Prabhu
  • 306
  • 2
  • 7
1

You may need to achieve an edit text behavior in Andorid TV were you need to make a non editable edit text but clickable, the below method helps you to achieve that -

/**
 * Disable edit text input skill
 */
    fun EditText.disableInput() {
        showSoftInputOnFocus = false
        keyListener = null // Make non editable
        // Hide keyboard when disabled edittext gets focus
        setOnFocusChangeListener { _, hasFocus ->
            if (hasFocus) hideKeyboard()
        }
    }

fun View.hideKeyboard() {
    val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}

You can get the click event using the conventional onClick listener thereafter

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
0

My requirement was to have an edittext look and feel and to open a pop up window on its click, but making enabled as false will not allow click listener on it. So I did it in this way.

 <EditText
        android:id="@+id/tvCustomerAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="80dp"
        android:layout_alignParentEnd="true"
        android:drawableRight="@drawable/arrow_down"
        android:inputType="none"
        android:focusable="false"
        android:clickable="true"
        tools:text="24"/>
Anupriya
  • 1,663
  • 3
  • 17
  • 28
0

I was unable to get rid of SoftKeyboard for the used EditText in the Tablet version and the following code snippet did the job very well.

eText.setEnabled(false);
eText.setFocusable(false);
eText.setActivated(false);
eText.setInputType(InputType.TYPE_NULL);
Harpreet
  • 2,990
  • 3
  • 38
  • 52
0

Set this attribute in your edittext.

<EditText
         android:editable="false"
     />
Marium Jawed
  • 391
  • 4
  • 9
0

If not necessary, try removing the inputType attribute. android:editable="false" works fine.

Twinkle
  • 37
  • 11