Reputation: 1732
How to set validation for EditText when user enters first character as a space i.e. " ". However I want to use space in all other cases. Only first character should not be a space. If user enters space then I'll disable the SEND button, other wise the SEND button will be enabled.
Could you please suggest. I tried with TextWatcher's onTextChanged method. But it's not working as expected.
Thanks.
Upvotes: 0
Views: 2660
Reputation: 1116
It should work with a TextWatcher, did you try something like that:
input.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {
String text = s.toString();
if(text.charAt(0) == ' ')
button.setClickable(false);
else
button.setClickable(true);
}
});
Upvotes: 0
Reputation: 28407
Please try with the following code:
mUiEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (charSequence.toString().startsWith(" ")) {
//disableButton(...)
} else {
//enableButton(...)
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
Upvotes: 2