How to make EditText not editable through XML in Android?

AndroidAndroid Edittext

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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());

Solution 3 - Android

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.

Solution 4 - Android

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"

Solution 5 - Android

disable from XML (one line):

 android:focusable="false"

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

editText.setFocusableInTouchMode(true);

Solution 6 - Android

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.

Solution 7 - Android

This combination worked for me:

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

Solution 8 - Android

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

Solution 9 - Android

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

        android:focusable="false"

Solution 10 - Android

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

Solution 11 - Android

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

Solution 12 - Android

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

Solution 13 - Android

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.

Solution 14 - Android

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>

Solution 15 - Android

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

Solution 16 - Android

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.

Solution 17 - Android

Try this :

android:inputType="none"

Solution 18 - Android

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"

Solution 19 - Android

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

Solution 20 - Android

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

Solution 21 - Android

Please try this!, it may help some people

<EditText  
android:enabled="false"
android:textColor="#000000"/>

Solution 22 - Android

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" />

Solution 23 - Android

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.

Solution 24 - Android

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

Solution 25 - Android

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();
               
            }
        });

Solution 26 - Android

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
        }
    }
}

Solution 27 - Android

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

Solution 28 - Android

Try

.setInputType(InputType.TYPE_NULL);

Solution 29 - Android

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

android:editable="false"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFran FitzpatrickView Question on Stackoverflow
Solution 1 - AndroidGalDude33View Answer on Stackoverflow
Solution 2 - AndroidKristiono SetyadiView Answer on Stackoverflow
Solution 3 - AndroidMahendranView Answer on Stackoverflow
Solution 4 - AndroidvidaView Answer on Stackoverflow
Solution 5 - AndroidkouretinhoView Answer on Stackoverflow
Solution 6 - AndroidTimuçinView Answer on Stackoverflow
Solution 7 - AndroidMassimo FazzolariView Answer on Stackoverflow
Solution 8 - AndroidAdorjan PrinczView Answer on Stackoverflow
Solution 9 - AndroidKai WangView Answer on Stackoverflow
Solution 10 - AndroidShahid SarwarView Answer on Stackoverflow
Solution 11 - AndroidaskimpView Answer on Stackoverflow
Solution 12 - Androiduser3185897View Answer on Stackoverflow
Solution 13 - AndroidsaricView Answer on Stackoverflow
Solution 14 - AndroidWoppiView Answer on Stackoverflow
Solution 15 - AndroidSuperuserView Answer on Stackoverflow
Solution 16 - AndroidjclafuenteView Answer on Stackoverflow
Solution 17 - AndroidRutvikView Answer on Stackoverflow
Solution 18 - AndroidLeoView Answer on Stackoverflow
Solution 19 - Androidjohngoche9999View Answer on Stackoverflow
Solution 20 - Androidnobjta_9x_tqView Answer on Stackoverflow
Solution 21 - AndroidRahul_Attili_007View Answer on Stackoverflow
Solution 22 - AndroidM-RazaviView Answer on Stackoverflow
Solution 23 - AndroidshubombView Answer on Stackoverflow
Solution 24 - AndroidAnbuselvan RockyView Answer on Stackoverflow
Solution 25 - AndroidJoão Scheleder NetoView Answer on Stackoverflow
Solution 26 - AndroidMoises PortilloView Answer on Stackoverflow
Solution 27 - Androidamir hossein kazemiView Answer on Stackoverflow
Solution 28 - Androidbenmakp benmaknView Answer on Stackoverflow
Solution 29 - Androiduser2309848View Answer on Stackoverflow