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.
Have you tried setText(java.lang.CharSequence, android.widget.TextView.BufferType) ? It's described as:
(emphasis mine)
You can try this:
This will completely disable EditText, disable long press if you don't want user to open edit text options.
this ensure the
EditText
control can't be selected and focused, so it can't be edited.I just tried this myself,
To disable editing text:
this also sets setFocusableInTouchMode to false!
To enable editing text:
this also sets setFocusable to true;
The only solution I have found for this scenario is to create 2 layouts. One is editable and one is not. You may have to create more than 2 layouts based on various conditions. Store the conditions in SharedPreferences or other means and load the relevant layout based on conditions after restarting the Activity. Here's an example:
in onCreate() of the activity:
The activity can be restarted based on testing for condition and calling functions as:
Hope this helps. There really has to be a better solution, but this is what I've been doing. Ideally, one would be able to do this on individual fields and not have to reload the activity / layouts. -bobby
I think an
InputFilter
that rejects all changes is a good solution:Edit: dlazar suggested (below) to change the
return
todst.subSequence(dstart, dend)
to overcome behavior that removes words.