In the layout you can set the EditText
widget to be non-editable via the android:editable attribute
.
How can I do this in code? I need to make the EditText
widget to be editable depending on conditions.
In the layout you can set the EditText
widget to be non-editable via the android:editable attribute
.
How can I do this in code? I need to make the EditText
widget to be editable depending on conditions.
The best way to do this is with this single line of code:
The docs say for this method:
I guess
through code
and
through xml.Also check this post on SO.
They should work and you can enable again programatically
Edittext.setEnabled(true);
I wanted to also point out an alternative solution that works nicely if you are creating new instances of an EditView. You can override the method getDefaultEditable() as suggested by the docs to return false. E.g.
I think the correct way to achieve the desired effect is:
If you want to switch between editable and non-editable you can do the following:
try this:
I do not see a related method for that attribute in the EditText class. However, there are other similar things you could use such as
android:focus/setFocusable(boolean)
or create another TextView whoseandroid:editable="false"
and usesetVisiblilty()
to switch between the editable and not editable views. If you useView.GONE
the user will never know there are two EditTexts.If your feeling ambitious you could probably do something with the EditText's
onTextChanged
listener like having it react with asetText
.