I have a layout where I have some edittexts and one button. This button is setEnabled(false). How I can change his status when user write some text in edittext? I want to when user put some text to edit text button setEnabled on true?
You need to use TextWatcher:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Its a good idea to also notify user why the Button is disabled.
Use a TextWatcher to disable/enable button and also set a error hint for EditText:
mEditText.addTextChangedListener(new EmptyValidator());
private class EmptyValidator implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (editable.length() == 0) {
mEditText.setError("Required");
mButton.setEnabled(false);
} else {
mEditText.setError(null);
mButton.setEnabled(true);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With