37

We can set editable property of EditText in XML layout but not programatically, but there is no setEditable() method!

If EditText is not Enabled [ by setEnabled(false)] it still Editable!

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vaibhav Jani
  • 12,428
  • 10
  • 61
  • 73
  • 1
    EditText.setEnabled(false); if you set this, the edittext disable.100% – RobinHood Jul 02 '11 at 06:37
  • How it possible, I checked, It disable your edittext. – RobinHood Jul 02 '11 at 06:56
  • Yes it may be disable but we cant copy value of that edittext using this. – Niranj Patel Jul 02 '11 at 07:02
  • The most reliable way to achieve that result is using `UI.setReadOnly(myEditText, true)` from [this library](https://github.com/delight-im/Android-BaseLib). There are a few properties that have to be set, which you can check out [in the source code](https://github.com/delight-im/Android-BaseLib/blob/62299c79d100e38627600907e755d563de072234/Source/src/im/delight/android/baselib/UI.java#L264). – caw Mar 09 '15 at 15:38
  • Possible duplicate of [How to replicate android:editable="false" in code?](http://stackoverflow.com/questions/660151/how-to-replicate-androideditable-false-in-code) – Ciro Santilli OurBigBook.com Feb 05 '16 at 22:43

13 Answers13

52

This may help:

if (cbProhibitEditPW.isChecked()) { // disable editing password
       editTextPassword.setFocusable(false);
       editTextPassword.setFocusableInTouchMode(false); // user touches widget on phone with touch screen
       editTextPassword.setClickable(false); // user navigates with wheel and selects widget
       isProhibitEditPassword= true;
} else { // enable editing of password
       editTextPassword.setFocusable(true);
       editTextPassword.setFocusableInTouchMode(true);
       editTextPassword.setClickable(true);
       isProhibitEditPassword= false;
}
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
JAL
  • 3,319
  • 2
  • 20
  • 17
  • The edit text will still receive key events after disable above, combine the answer with Sumit Sonome answer below to fully make it non-editable. – Thecarisma Feb 22 '20 at 14:32
23

I did it in a easier way , setEditable and setFocusable false. but you should check this.

How to replicate android:editable="false" in code?

Community
  • 1
  • 1
sat
  • 40,138
  • 28
  • 93
  • 102
9

Fetch the KeyListener value of EditText by editText.getKeyListener() and store in the KeyListener type variable, which will contain the Editable property value:

KeyListener variable;
variable = editText.getKeyListener(); 

Set the Editable property of EditText to false as:

 edittext.setKeyListener(null);

Now set Editable property of EditText to true as:

editText.setKeyListener(variable);  

Note: In XML the default Editable property of EditText should be true.

sfmirtalebi
  • 370
  • 7
  • 16
Sumit Sonone
  • 111
  • 1
  • 3
6

How to do it programatically :

To enable EditText use:

et.setEnabled(true);

To disable EditText use:

et.setEnabled(false);
Karan Sharma
  • 2,353
  • 4
  • 28
  • 46
5

hope this one helps you out:

edittext1.setKeyListener(null);
edittext1.setCursorVisible(false);
edittext1.setPressed(false);
edittext1.setFocusable(false);
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
  • This is my preferred solution, plus `setInputType(InputType.TYPE_NULL)` as some other solutions also disable scrolling as well as editing – David O'Meara Jun 03 '21 at 04:22
5
editText.setInputType(InputType.TYPE_NULL);
Andrei
  • 42,814
  • 35
  • 154
  • 218
5

Once focus of edit text is removed, it would not allow you to type even if you set it to focusable again.

Here is a way around it

if (someCondition)
   editTextField.setFocusable(false);
else
   editTextField.setFocusableInTouchMode(true);

Setting it true in setFocusableInTouchMode() seems to do the trick.

Tushar Thakur
  • 956
  • 3
  • 15
  • 32
4

try this,

EditText editText=(EditText)findViewById(R.id.editText1);

editText.setKeyListener(null);

It works fine...

sandeepmaaram
  • 4,191
  • 2
  • 37
  • 42
2

Try this it is working fine for me..

EditText.setInputType(0);
EditText.setFilters(new InputFilter[] {new InputFilter()
{
@Override
public CharSequence filter(CharSequence source, int start,
                            int end, Spanned dest, int dstart, int dend) 
{
return source.length() < 1 ? dest.subSequence(dstart, dend) : "";

}
}
});
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
1
editText.setFocusable(false);
editText.setClickable(false);
kiran kumar
  • 1,402
  • 17
  • 34
1

Since setEditable(false) is deprecated, use textView.setKeyListener(null); to make editText non-clickable.

Amita
  • 195
  • 1
  • 1
  • 13
1

Since the setEditable(false) is deprecated and we can't use it programmatically, we can use another way to solve it with setInputType(InputType.TYPE_NULL)

It means we change the input type of edit text. We set it to NULL so it becomes not editable.

Here's the sample that might be useful (I code this on my onCreateView method Fragment):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.yourfragment, container, false);
    EditText sample = view.findViewById(R.id.youredittext);
    sample.setInputType(InputType.TYPE_NULL);

Hope this will answer the problem

0

An easy and safe method:

editText.clearFocus();
editText.setFocusable(false);
drakeet
  • 2,685
  • 1
  • 23
  • 30