311

Can anyone tell me how to make an EditText not editable via XML? I tried setting android:editable to false, but

  1. it is deprecated; and
  2. it didn't work.
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Fran Fitzpatrick
  • 17,902
  • 15
  • 33
  • 34
  • 6
    If not editable, why would you need the EditText? Wont using an ordinary TextView be enough? And is setEditable() function deprecated too? – kiki Oct 14 '10 at 04:52
  • 4
    For me this makes sense in order to enable standard copy paste for the contents. With a TextView it's not possible (at least not default behaviour) – User Apr 30 '12 at 15:01
  • 1
    Correction: It's a bad idea. On certain circunstances the view becomes editable, or the copy paste options doesn't show anymore. Will look for other approach. – User Apr 30 '12 at 15:09
  • 11
    In my case, I want to temporarily disable an EditText while a set of operations performs, and then reenable it when the operation finishes. – Joe Plante Sep 10 '12 at 15:51
  • 1
    This question is about a legitimate concern. To preserve the `EditText` style and permit scrolling, use `UI.setReadOnly(myEditText, true)` from [this library](https://github.com/delight-im/Android-BaseLib). There are a few properties that have to be set, which you can check out [in the source code](https://github.com/delight-im/Android-BaseLib/blob/62299c79d100e38627600907e755d563de072234/Source/src/im/delight/android/baselib/UI.java#L264). – caw Mar 09 '15 at 15:37
  • 1
    @kiki: TextView elements are not as big as EditText. – Don Larynx May 16 '15 at 04:06
  • Linking this question too: https://stackoverflow.com/questions/9470171/edittext-not-editable – dazza5000 Mar 09 '16 at 23:11

29 Answers29

415

Add this to your EditText xml file:

<EditText ...
        android:clickable="false" 
        android:cursorVisible="false" 
        android:focusable="false" 
        android:focusableInTouchMode="false">
</EditText>

It will do the same effect as android:editable="false". Worked for me, hope it'll work for you too.

CaptJak
  • 3,592
  • 1
  • 29
  • 50
GalDude33
  • 7,071
  • 1
  • 28
  • 38
  • 3
    notes.setKeyListener(null); notes.setClickable(false); notes.setCursorVisible(false); notes.setFocusable(false); notes.setFocusableInTouchMode(false); – Jack Franzen Aug 18 '14 at 04:34
  • 4
    If you want to do it in the XML ... what will be the difference between it and TextView ??? – Chris Sim Sep 02 '14 at 08:15
  • 12
    The difference is It can be used with `TextInputLayout` – ahmadalibaloch Jun 03 '16 at 13:15
  • Keep in mind your EditText won't be usable by keyboards or D-Pads if you set it not focusable. – Renato Jul 20 '16 at 23:28
  • 2
    This should be marked as the solution, as it is a pure XML solution as requested in the question. – zgc7009 Nov 03 '16 at 21:14
  • After use all of the answer to given this question . **This one be perfectly worked for me .** – Bhavin Patel Jan 25 '17 at 12:14
  • It might work, but it won't do the same effect as `editable="false"`. This setting doesn't disable focus or clickability. If I try to set listeners for when the EditText is focused/clicked, but I don't want to allow the user to manually edit it? – Gabriel Vasconcelos Mar 04 '17 at 15:05
  • It's still has flaw, confirmed on device Redmi, by repeating tap, can still show up select-copy-paste mini-dialog. Add `enabled=false` to resolve it. – Conan Jul 20 '17 at 06:41
  • There is not a better way?, Why change 1 line to 4. Because in older version the xml annotation `android:editable = false` did it. – Canatto Filipe Oct 27 '17 at 12:34
  • this worked for me, and to make it like a clickable. I just removed `android:clickable="false"` from your solution to do the editing on another fragment/acitivity. – Ric17101 Aug 06 '18 at 07:00
  • 2
    This is deprecated (October 2018) – Sebastian Paduano Oct 22 '18 at 12:50
  • 1
    That does NOT has the same effect as setting editable to false, with the last you can still click on the control and select and copy its contents. – Fran Marzoa Jun 17 '20 at 13:18
413

Use this simple code:

textView.setKeyListener(null);

It works.

Edit : To add KeyListener later, do following

1 : set key listener to tag of textView

textView.setTag(textView.getKeyListener());
textView.setKeyListener(null);

2 : get key listener from tag and set it back to textView

textView.setKeyListener((KeyListener) textView.getTag());
ELITE
  • 5,815
  • 3
  • 19
  • 29
Kristiono Setyadi
  • 5,635
  • 1
  • 19
  • 29
  • 31
    @AnkitAwasthi textView.setTag(textView.getKeyListener()); set it back later with textView.setKeyListener((KeyListener)textView.getTag()); – Thomas Dignan Jun 20 '12 at 13:27
  • @TomDignan great way easily to that! I do have one question. Does this in any way change the default values needed by KeyListener, like `setFocusable()` because the doc says that the default implementation might be changed if changed. – Andy Jun 26 '12 at 18:20
  • afaik no, the tag should be available for you to use – Thomas Dignan Jun 29 '12 at 04:55
  • It works, but softkeyboard does not show "Next" key which would easily navigate focus to next editable text. How would you fix that? – alimg Jun 13 '13 at 13:38
  • I would suggest to edit XML file and set : `android:enabled="false"` – l1sq0u48 Jan 22 '14 at 15:25
  • Good solution for when you need EditText as is (i.e. focusable and clickable) but are using something other than keyboard to enter characters. +1 – Dennis K Jan 28 '14 at 01:57
  • 41
    The question was about setting it via *XML*, why is this marked as solution? – Denys Vitali Apr 21 '16 at 07:25
  • Completely agree with this answer. However, the EditText's cursor disappeared with this method on my side. I also toggled the cursor visibility ending with this code: Disabling: `editText.setCursorVisible(false);` `editText.setTag(editText.getKeyListener());` `editText.setKeyListener(null);` Enabling: `editText.setKeyListener((KeyListener) editText.getTag());` `editText.setCursorVisible(true);` – Doc_1faux Jan 27 '23 at 14:13
55

Let your Edittext be

EditText editText;

editText.setKeyListener(null);

will work but it just not listening to keys.

User can see a cursor on the edittext.

You can try editText.setEnabled(false);

That means user cannot see the cursor. To simply say it will became TextView.

Mahendran
  • 2,719
  • 5
  • 28
  • 50
  • 3
    It's correct since EditText is a subclass of TextView. However, please use editText as a variable instead of textView. – Joe Plante Sep 10 '12 at 15:49
  • @JoePlante Changed variable name thanks for constructive comment. – Mahendran Sep 11 '12 at 05:36
  • this grays out the EditText – stephen Feb 15 '13 at 17:23
  • Grayed EditText indicates that its is disabled and user can't edit it. – Mahendran Feb 18 '13 at 05:21
  • Settting the colour as well as will prevent the grayed appearance. XML: Try android:textColor="@color/Black" android:enabled="false" – Red wine Aug 26 '18 at 00:45
  • @Redwine yes, that could work. IMO, gray out convey item in disabled state. If at all the EditText will be enabled based on another action (a button click?), user will come to know the correlation between those UI components. – Mahendran Aug 27 '18 at 06:05
34

As mentioned in other answers, you can do a setEnabled(false) but since you are asking how to set it via XML, here is how to do it.

Add the following attribute to your EditText:

android:enabled="false"
vida
  • 4,459
  • 1
  • 22
  • 27
25

disable from XML (one line):

 android:focusable="false"

re-enable from Java, if need be (also one line):

editText.setFocusableInTouchMode(true);
kouretinho
  • 2,190
  • 1
  • 23
  • 37
  • Good Answer. This way you can still set editText value using editText.setText("your text"). But user can't edit it. – Prakash Feb 04 '16 at 19:15
19

They made "editable" deprecated but didn't provide a working corresponding one in inputType.

By the way, does inputType="none" has any effect? Using it or not does not make any difference as far as I see.

For example, the default editText is said to be single line. But you have to select an inputType for it to be single line. And if you select "none", it is still multiline.

Timuçin
  • 4,653
  • 3
  • 25
  • 34
  • 1
    It does not make a difference on _all_ devices, but it _should_, according to the docs: http://developer.android.com/reference/android/widget/TextView.html#setInputType(int) – caw Sep 08 '13 at 06:12
  • 14
    A beautiful example of Google mess – Yar Feb 07 '15 at 18:46
15

This combination worked for me:

<EditText ... >
    android:longClickable="false"
    android:cursorVisible="false"
    android:focusableInTouchMode="false"
</EditText>
Massimo Fazzolari
  • 5,185
  • 3
  • 27
  • 36
13

As you mentioned android:editable is deprecated. android:inputType="none" should be used instead but it does not work due to a bug (https://code.google.com/p/android/issues/detail?id=2854)

But you can achieve the same thing by using focusable.

Via XML:

<EditText ...
        android:focusable="false" 
</EditText>

From code:

((EditText) findViewById(R.id.LoginNameEditText)).setFocusable(false);
ar34z
  • 2,609
  • 2
  • 24
  • 37
Adorjan Princz
  • 11,708
  • 3
  • 34
  • 25
  • Courtesy of google, the bug will fall into two different scenario: 1.) It will be fix but after 6 years 2.) It will never be fix, they'll just develop new version of Android. – Neon Warge Jul 20 '16 at 02:29
13

if you want to click it, but not want to edit it, try:

        android:focusable="false"
Kai Wang
  • 3,303
  • 1
  • 31
  • 27
9

Just use android:enabled="false". This will also give an appearance to the EditText which will make it look not editable.

Asif Patel
  • 1,744
  • 1
  • 20
  • 27
Shahid Sarwar
  • 1,209
  • 14
  • 29
7
<EditText
    android:id="@+id/txtDate"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="2dp"
    android:clickable="false"
    android:cursorVisible="false"
    android:gravity="center" />

and use following :

txtDate.setKeyListener(null);
askimp
  • 155
  • 3
  • 10
7

If you want to do it in java code just use this line to disable it:

editText.setEnabled(false);

And this to enable it:

editText.setEnabled(true);
veducm
  • 5,933
  • 2
  • 34
  • 40
user3185897
  • 81
  • 1
  • 1
5

I tried to do:

textView.setInputType( InputType.TYPE_NULL );

which should work, but for me it did not.

I finished with this code:

textView.setKeyListener(new NumberKeyListener() {
    public int getInputType() {
        return InputType.TYPE_NULL;
    }

    protected char[] getAcceptedChars() {
        return new char[] {};
    }
});

which works perfectly.

saric
  • 1,044
  • 4
  • 14
  • 28
4

When I want an activity to not focus on the EditText and also not show keyboard when clicked, this is what worked for me.

Adding attributes to the main layout

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

and then the EditText field

android:focusable="false"
android:focusableInTouchMode="false"

Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_home"
    android:background="@drawable/bg_landing"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    tools:context="com.woppi.woppi.samplelapp.HomeActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/fromEditText"
        android:layout_weight="1"
        android:hint="@string/placeholder_choose_language"
        android:textColor="@android:color/white"
        android:textColorHint="@android:color/darker_gray"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:onClick="onSelectFromLanguage" />

</RelativeLayout>
Woppi
  • 5,303
  • 11
  • 57
  • 81
3

In addition to @mahe madi you can try this as well

editText.setEnabled(false);
editor.setTextColor(Color.BLACK);

This method will hide cursor, lose focus and more importantly set text color to black behaving like TextView.

And to revert Back to editText simply do this to gain all the properties of EditText.

editText.setEnabled(true);

Hope it Helps :)

Superuser
  • 91
  • 1
  • 4
3

You could use android:editable="false" but I would really advise you to use setEnabled(false) as it provides a visual clue to the user that the control cannot be edited. The same visual cue is used by all disabled widgets and consistency is good.

yprez
  • 14,854
  • 11
  • 55
  • 70
jclafuente
  • 193
  • 1
  • 5
3

Try this :

android:inputType="none"
Bannings
  • 10,376
  • 7
  • 44
  • 54
Rutvik
  • 137
  • 1
  • 1
  • 9
3

This way did the job for me, i can selec and copy to clipboard, but i can't modify or paste.

android:enable="true"
android:textIsSelectable="true"
android:inputType="none"
Leo
  • 71
  • 1
2

I use EditText.setFocusable(false) and set true again if I want to edit.

nobjta_9x_tq
  • 1,205
  • 14
  • 16
2

Please try this!, it may help some people

<EditText  
android:enabled="false"
android:textColor="#000000"/>
Joundill
  • 6,828
  • 12
  • 36
  • 50
  • Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes – Ran Marciano Feb 09 '21 at 06:16
2

I've tried the following:

codeEditText.setInputType(InputType.TYPE_NULL);

this.codeEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

  @Override
  public void onFocusChange(View v, boolean hasFocus) {

    if (hasFocus) {

      pickCode();

    }

  }

});
this.codeEditText.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {

    pickCode();

  }

});

but the problem was that if the edit text is the first in the form then it gets the focus and the pickCode() code which launches a new activity is called straight away. So I modified the code as follows and it seems to work quite well (except I cannot set the focus on the text edit but I don't need to):

itemCodeEditText.setFocusable(false);

this.itemCodeEditText.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {

    pickItem();

  }

});

Best Regards,

Comments welcome,

John Goche

johngoche9999
  • 1,641
  • 2
  • 14
  • 21
  • 1
    Seems to work fine, but isn't it easier to use TextView which looks like EditText (using : style="@android:style/Widget.EditText" ) ? – android developer Dec 31 '14 at 09:31
1

android:editable is deprecated so use inputType instead.

   <EditText
        android:id="@+id/editText_x"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="70"
        android:inputType="none"
        android:hint="@string/enter_x" />
M-Razavi
  • 3,327
  • 2
  • 34
  • 46
  • android:inputType="none" is much different from android:editable, try it yourself! – Denys Vitali Apr 21 '16 at 07:33
  • 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. – M-Razavi Apr 21 '16 at 11:32
  • Your suggestion doesn't apply for an EditText with a PopupWindow, see [this image](http://i.imgur.com/h2D37xJ.png) to better understand the problem. In this example I do want the look of an EditText but I have to "lock it" in order to let the user change the value just by the popupwindow. [Check this issue](https://code.google.com/p/android/issues/detail?id=187185) to see more motivation. It's a problem that is around since 2009. – Denys Vitali Apr 21 '16 at 15:15
1

I have faced same problem but after observation I found that android:editable="false" is only working when inputtype (android:inputType="ANY_VALUE") is not given in the Edittext.

Remove inputType from EditText and use editable = false, its working fine.

shubomb
  • 672
  • 7
  • 20
1

Short answer: I used android:focusable="false" and it worked. Click listening is also working now.

Anbuselvan Rocky
  • 606
  • 6
  • 22
0

I resolve this with a dialog.

    AlertDialog dialog;
    EditText _editID;
    TextView _txtID;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        _txtID = (TextView) findViewById(R.id._txtID);
        dialog = new AlertDialog.Builder(this).create();
        _editID = new EditText(this);
        _editID.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
        dialog.setTitle("Numero do Registro:");
        dialog.setView(_editID);
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "IR", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                _txtID.setText(_editID.getText());
                toId(Integer.parseInt((_editID.getText().toString())));
            }
        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCELAR", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        _txtID.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _txtID.setText("_editID.getText()");
                //_editID.setText("");
                dialog.show();

            }
        });
0

Kotlin extension option:

fun AppCompatAutoCompleteTextView.setEditable(editable: Boolean) {
    if (!editable) {
        this.tag = this.keyListener
        this.keyListener = null
    } else {
        if(this.tag is KeyListener) {
            this@setEditable.keyListener = this@setEditable.tag as KeyListener
        }
    }
}
Moises Portillo
  • 828
  • 8
  • 12
0

You can toggle between editable of edittext by this method:

public void toggleEditableOfEditText(EditText editText){
        if (editText.getKeyListener() != null) {
            editText.setTag(editText.getKeyListener());
            editText.setKeyListener(null);
        } else {
            editText.setKeyListener((KeyListener) editText.getTag());
        }
    }

and then use this method to make edittext editable or not editable:

toggleEditableOfEditText(editText);
-1

Try

.setInputType(InputType.TYPE_NULL);
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
-10

Try this code. It's working in my project, so it will work in your project.

android:editable="false"
Sandy Chapman
  • 11,133
  • 3
  • 58
  • 67
  • 2
    I can confirm that this not works and what's more using depreciated method is not recommended. – Adorjan Princz May 08 '13 at 21:46
  • 1
    android:editable is actually the best solution if you want the field to be clickable but not editable, i.e. to open a dialog after clicking on it. – Yar Feb 07 '15 at 18:44
  • 1
    not because it works on your project it will on ours. Very poor thinking. – Neon Warge Jul 20 '16 at 02:31