EditText non editable
Asked Answered
T

18

86

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?

Teenager answered 27/2, 2012 at 18:26 Comment(4)
know why does not work only with android:editable="false". Thanks.Teenager
@Zortkun: That is exactly what the OP is asking.Vaporizer
@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.Vaporizer
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 allTeenager
S
205

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.

Sternway answered 27/2, 2012 at 18:40 Comment(6)
Thanks with alternative work tine but android:editable="false" noTeenager
Use of textview is better in this caseDecasyllabic
The below answer of setting android:enabled="false" works better if you also want the input to be greyed out.Carri
android:inputType="none" still allows you to type textSaccharate
Agree with @Saccharate . android:inputType="none" still allows user to type text with another solution. (e.g, hardware keyboard)Ratty
You can use a TextView like others have said, but set the style of it to be the EditText style.Miskolc
J
35

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

 android:inputType="none"
 android:enabled="false"
Justin answered 14/3, 2018 at 10:10 Comment(2)
This should be the accepted answer. Using android:inputType="none" won't be what you are expecting unless you add android:enabled="false"Caspar
This. Setting just either inputType="none" or enabled="false" doesn't seem to do anything.. must be both!Roana
F
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);
Falsework answered 30/8, 2017 at 15:31 Comment(0)
H
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

Hydrogenolysis answered 19/5, 2017 at 12:13 Comment(3)
Since editable is deprecated use this instead: ` android:inputType="none"`(along with the other ones you have).Freund
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...Casta
User is still able to use paste option to change text of the EditText.Cranwell
H
9

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

Hurless answered 23/1, 2022 at 11:3 Comment(0)
O
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.

Ovolo answered 23/10, 2018 at 12:27 Comment(0)
S
3

Add below line in youe xml edittext.

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

Its working for me

Stepfather answered 22/3, 2018 at 12:20 Comment(0)
P
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.

Phosgene answered 29/8, 2018 at 7:40 Comment(0)
J
2
android:enabled = false

Include that line in your layout xml file.

Jaala answered 30/10, 2019 at 8:13 Comment(0)
E
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>
Evenhanded answered 6/11, 2019 at 16:29 Comment(0)
R
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"
Ronni answered 5/9, 2020 at 6:40 Comment(1)
Used this if you have an icon to be clickable, this works for me.Collyrium
R
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" />
Rimini answered 25/3, 2018 at 13:28 Comment(0)
T
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!

Traject answered 12/3, 2020 at 16:53 Comment(0)
S
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

Sihunn answered 9/12, 2021 at 6:38 Comment(0)
F
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"/>
Firewarden answered 19/7, 2019 at 12:59 Comment(0)
B
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);
Bitten answered 27/12, 2019 at 12:3 Comment(0)
L
0

Set this attribute in your edittext.

<EditText
         android:editable="false"
     />
Livraison answered 6/2, 2020 at 6:43 Comment(0)
A
0

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

Ankeny answered 5/6, 2020 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.