TIP: How to Programatically Enable/Disable Editing in a EditText Box
In this TIP we discuss how to programatically enable and disable user edits in an existing EditText box. The initial setting can be set using the XML attribute android:editable to false, but this does not prevent the EditText box from getting the focus!
Sometimes you just want to programmatically enable or disable the ability for the user to edit text in an EditText widget. Given a boolean flag to keep track of this property as:
private boolean isProhibitEditPassword= false;
Here is some code that will enable/disable user input by allowing/refusing to let the user access the EditText widget.
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;
}
Hope that helps,
JAL