Reputation: 595
I have an Editext where i'm performing validation that is working well but on pressing space msg got disapper.Can anyone please point out where we are doing wrong.
Please find the image below for more clarity :
The following code we are using:
txtFullName.addTextChangedListener(new MyTextWatcher(txtFullName));
txtEmail.addTextChangedListener(new MyTextWatcher(txtEmail));
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
public void afterTextChanged(Editable editable) {
switch (view.getId()) {
case R.id.txtFullName:
if (txtFullName.getText().toString().length() >= 10) {
txtFullName.setError(getString(R.string.limit_exceeds));
txtFullName.requestFocus();
}
break;
case R.id.txtEmail:
if (txtEmail.getText().toString().length() >= 20) {
txtEmail.setError(getString(R.string.limit_exceeds));
txtEmail.requestFocus();
}
break;
default:
break;
}
}
Upvotes: 0
Views: 131
Reputation: 12477
Original answer
According to the screenshot, the entered text is 10
characters.
Pressing space adds one more character making txtEmail.getText().toString().length()
to be 11
, and triggers the TextWatcher, which only displays error if the text is 10 characters or shorter.
Upvotes: 1