How to make EditText not editable through XML in Android?
Asked Answered
I

29

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.
Inhospitality answered 13/10, 2010 at 22:52 Comment(7)
If not editable, why would you need the EditText? Wont using an ordinary TextView be enough? And is setEditable() function deprecated too?Bandanna
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)Accouchement
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.Accouchement
In my case, I want to temporarily disable an EditText while a set of operations performs, and then reenable it when the operation finishes.Condolent
This question is about a legitimate concern. To preserve the EditText style and permit scrolling, use UI.setReadOnly(myEditText, true) from this library. There are a few properties that have to be set, which you can check out in the source code.Brawley
@kiki: TextView elements are not as big as EditText.Apivorous
Linking this question too: https://mcmap.net/q/101313/-edittext-non-editableImperishable
P
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());
Pentastich answered 30/5, 2011 at 9:23 Comment(8)
@AnkitAwasthi textView.setTag(textView.getKeyListener()); set it back later with textView.setKeyListener((KeyListener)textView.getTag());Dallis
@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.Apiculture
afaik no, the tag should be available for you to useDallis
It works, but softkeyboard does not show "Next" key which would easily navigate focus to next editable text. How would you fix that?Fraser
I would suggest to edit XML file and set : android:enabled="false"Gabriello
Good solution for when you need EditText as is (i.e. focusable and clickable) but are using something other than keyboard to enter characters. +1Attest
The question was about setting it via XML, why is this marked as solution?Outshout
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);Erichericha
N
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.

Nan answered 21/10, 2011 at 17:24 Comment(12)
notes.setKeyListener(null); notes.setClickable(false); notes.setCursorVisible(false); notes.setFocusable(false); notes.setFocusableInTouchMode(false);Ichthyo
If you want to do it in the XML ... what will be the difference between it and TextView ???Efta
The difference is It can be used with TextInputLayoutMccallion
Keep in mind your EditText won't be usable by keyboards or D-Pads if you set it not focusable.Comfrey
This should be marked as the solution, as it is a pure XML solution as requested in the question.Heeled
After use all of the answer to given this question . This one be perfectly worked for me .Elegiac
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?Emulsifier
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.Phosphorous
There is not a better way?, Why change 1 line to 4. Because in older version the xml annotation android:editable = false did it.Ditchwater
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.Administrator
This is deprecated (October 2018)Royceroyd
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.Lilley
P
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());
Pentastich answered 30/5, 2011 at 9:23 Comment(8)
@AnkitAwasthi textView.setTag(textView.getKeyListener()); set it back later with textView.setKeyListener((KeyListener)textView.getTag());Dallis
@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.Apiculture
afaik no, the tag should be available for you to useDallis
It works, but softkeyboard does not show "Next" key which would easily navigate focus to next editable text. How would you fix that?Fraser
I would suggest to edit XML file and set : android:enabled="false"Gabriello
Good solution for when you need EditText as is (i.e. focusable and clickable) but are using something other than keyboard to enter characters. +1Attest
The question was about setting it via XML, why is this marked as solution?Outshout
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);Erichericha
S
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.

Solidus answered 7/2, 2012 at 5:53 Comment(6)
It's correct since EditText is a subclass of TextView. However, please use editText as a variable instead of textView.Condolent
@JoePlante Changed variable name thanks for constructive comment.Solidus
this grays out the EditTextPriory
Grayed EditText indicates that its is disabled and user can't edit it.Solidus
Settting the colour as well as will prevent the grayed appearance. XML: Try android:textColor="@color/Black" android:enabled="false"Trim
@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.Solidus
V
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"
Vicechairman answered 21/7, 2014 at 13:58 Comment(5)
exact answer to the OPs question.Counterfeit
but this method deprecatedGhiberti
@Lalitkumar enabled is not deprecated. editable is.Scar
by enable, we cant access the click listener as well. so for me its not the answer.Confiture
this is hide text :(Tactless
H
25

disable from XML (one line):

 android:focusable="false"

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

editText.setFocusableInTouchMode(true);
Hiddenite answered 6/2, 2014 at 17:28 Comment(1)
Good Answer. This way you can still set editText value using editText.setText("your text"). But user can't edit it.Stallings
S
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.

Schaffner answered 20/10, 2010 at 2:14 Comment(2)
It does not make a difference on all devices, but it should, according to the docs: developer.android.com/reference/android/widget/…Brawley
A beautiful example of Google messAugur
G
15

This combination worked for me:

<EditText ... >
    android:longClickable="false"
    android:cursorVisible="false"
    android:focusableInTouchMode="false"
</EditText>
Galliot answered 12/6, 2017 at 9:21 Comment(0)
R
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);
Renoir answered 8/5, 2013 at 20:20 Comment(1)
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.Gessner
S
13

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

        android:focusable="false"
Snubnosed answered 11/3, 2016 at 20:43 Comment(0)
K
9

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

Kilby answered 14/6, 2016 at 7:18 Comment(0)
L
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);
Lusk answered 26/9, 2012 at 7:31 Comment(0)
H
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);
Hoof answered 27/1, 2014 at 19:49 Comment(0)
A
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.

Agentive answered 5/4, 2011 at 10:56 Comment(0)
M
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>
Mapel answered 17/11, 2016 at 11:55 Comment(0)
F
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 :)

Frazzle answered 30/12, 2013 at 10:24 Comment(0)
D
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.

Danyelldanyelle answered 20/1, 2014 at 15:4 Comment(0)
K
3

Try this :

android:inputType="none"
Kuo answered 20/9, 2015 at 21:4 Comment(0)
K
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"
Karlin answered 3/11, 2016 at 21:7 Comment(0)
B
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

Ballyrag answered 15/2, 2012 at 15:47 Comment(1)
Seems to work fine, but isn't it easier to use TextView which looks like EditText (using : style="@android:style/Widget.EditText" ) ?Gainsay
K
2

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

Kolodgie answered 25/6, 2015 at 3:27 Comment(0)
D
2

Please try this!, it may help some people

<EditText  
android:enabled="false"
android:textColor="#000000"/>
Demetriusdemeyer answered 9/2, 2021 at 6:0 Comment(1)
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 upvotesDeclaratory
R
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" />
Raisin answered 11/9, 2015 at 16:20 Comment(3)
android:inputType="none" is much different from android:editable, try it yourself!Outshout
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.Raisin
Your suggestion doesn't apply for an EditText with a PopupWindow, see this image 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 to see more motivation. It's a problem that is around since 2009.Outshout
U
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.

Unassailable answered 16/12, 2016 at 9:26 Comment(0)
K
1

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

Keister answered 26/4, 2020 at 9:51 Comment(0)
U
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();

            }
        });
Umlaut answered 27/3, 2020 at 21:30 Comment(0)
I
0

Kotlin extension option:

fun AppCompatAutoCompleteTextView.setEditable(editable: Boolean) {
    if (!editable) {
        this.tag = this.keyListener
        this.keyListener = null
    } else {
        if(this.tag is KeyListener) {
            [email protected] = [email protected] as KeyListener
        }
    }
}
Isochroous answered 12/1, 2021 at 2:24 Comment(0)
M
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);
Mousetrap answered 5/1, 2022 at 18:35 Comment(0)
S
-1

Try

.setInputType(InputType.TYPE_NULL);
Senter answered 26/11, 2015 at 8:12 Comment(0)
W
-10

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

android:editable="false"
Weichsel answered 23/4, 2013 at 5:20 Comment(3)
I can confirm that this not works and what's more using depreciated method is not recommended.Renoir
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.Augur
not because it works on your project it will on ours. Very poor thinking.Gessner

© 2022 - 2024 — McMap. All rights reserved.