EditText non editable

AndroidAndroid Edittext

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

 android:inputType="none"
 android:enabled="false"

Solution 3 - Android

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

Solution 4 - Android

 <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

Solution 5 - Android

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.

Solution 6 - Android

Add below line in youe xml edittext.

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

Its working for me

Solution 7 - Android

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.

Solution 8 - Android

android:enabled = false

Include that line in your layout xml file.

Solution 9 - Android

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>

Solution 10 - Android

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

Solution 11 - Android

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

Solution 12 - Android

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"

Solution 13 - Android

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

Solution 14 - Android

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

Solution 15 - Android

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

Solution 16 - Android

Set this attribute in your edittext.

<EditText
         android:editable="false"
     />

Solution 17 - Android

android:inputType="none"
android:enabled="false"
android:clickable="false"

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

Solution 18 - Android

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

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
QuestionJjreinaView Question on Stackoverflow
Solution 1 - AndroidJean-Philippe RoyView Answer on Stackoverflow
Solution 2 - AndroidAkshayTView Answer on Stackoverflow
Solution 3 - AndroidvovahostView Answer on Stackoverflow
Solution 4 - AndroidDharmendra MishraView Answer on Stackoverflow
Solution 5 - AndroidMofe EjegiView Answer on Stackoverflow
Solution 6 - AndroidDeepak JainView Answer on Stackoverflow
Solution 7 - AndroidGourav SamreView Answer on Stackoverflow
Solution 8 - AndroidSuneet SrivastavaView Answer on Stackoverflow
Solution 9 - AndroidAndré RamonView Answer on Stackoverflow
Solution 10 - AndroidOlayiwola OshoView Answer on Stackoverflow
Solution 11 - AndroidCogiat84View Answer on Stackoverflow
Solution 12 - AndroidRam ChhabraView Answer on Stackoverflow
Solution 13 - AndroidAnoop M MaddasseriView Answer on Stackoverflow
Solution 14 - AndroidAnupriyaView Answer on Stackoverflow
Solution 15 - AndroidHarpreetView Answer on Stackoverflow
Solution 16 - AndroidMarium JawedView Answer on Stackoverflow
Solution 17 - AndroidPrabhuView Answer on Stackoverflow
Solution 18 - AndroidTwinkleView Answer on Stackoverflow