Android Studio:EditText editable is deprecated How to use inputType
Asked Answered
S

9

29

I want to realize

android:editable="false"

But it told me editable is deprecated ,you can use inputType instead.

So I don't know how to realize it by using inputType.

Skylark answered 24/1, 2017 at 13:37 Comment(4)
https://mcmap.net/q/101313/-edittext-non-editableKinglet
you should try android:inputType="none"Alkalize
Possible duplicate of EditText not editableIlo
FYI: I found on my app that when inputType="none", it still showed a cursor on the field. I had to set editable="false" to make it go away, anyway.Charmer
F
21

Use

android:clickable="false"

. but In case you want to use onclick listener for it. don't use

android:clickable="false"

.. Use

android:cursorVisible="false" android:focusable="false" .

Flageolet answered 14/1, 2019 at 16:15 Comment(0)
E
14

The Code

In XML

<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Hint"
    android:focusable="false"
    android:clickable="false"
    android:cursorVisible="false"
    />

The Result

enter image description here

Java Code

You can achieve the same result at runtime with this code

EditText et = findViewById(R.id.myEditText);
et.setFocusable(false);
et.setClickable(false);
et.setCursorVisible(false);
Ethelinda answered 11/2, 2020 at 0:47 Comment(0)
T
4

If you are using any input type that is not kind of text or number like date or date and time you can use android:inputType="date" or android:inputType="datetime" or android:inputType="time". If you are not using any such kind of input type you can use android:inputType="none".

Three answered 10/8, 2017 at 5:41 Comment(0)
C
3

All the answers above suggesting XML markup changes doesnt seem to work. However I managed to achieve a similar result through code.

Simply use:

editText.setKeyListener(null);

Cygnus answered 6/9, 2017 at 10:36 Comment(0)
E
1

Use this if you wanted to use click of edittext and disable input

android:clickable="false" 
android:cursorVisible="false" 
android:focusable="false" 
android:focusableInTouchMode="false"
Ennis answered 11/3, 2021 at 12:10 Comment(0)
C
0

You can control behavior of EditText from TextWatcher

@Override
public void onTextChanged(String text) {
     setEditTextCurrentValue((isEditTextEditable())?text:getEditTextCurrentValue());
}

So user can enter to the control, can copy its content. But he can't change it if you forbid.

Chronoscope answered 25/8, 2020 at 11:55 Comment(0)
H
0

Use

android:inputType="none"

instead of

android:editable="false"
Henig answered 23/9, 2020 at 17:38 Comment(1)
this does nothing, still able to enter textAsaasabi
L
0

well input type doesn't work the way you expect it to work when you use inputType=none

but you could use this to make your editText not editable which is what I think you want to achieve

<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

Lobo answered 23/1, 2022 at 10:57 Comment(0)
F
0

Can be set it in code like this:

myEditText.inputType = InputType.TYPE_NULL
Ferrule answered 14/6, 2023 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.