0

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 : enter image description here

After pressing space bar: enter image description here

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;
            }
     }
Pranesh Sahu
  • 595
  • 5
  • 26
  • What you should be doing is something like [this](http://www.materialdoc.com/character-counter/) – Sufian Jul 06 '17 at 15:59
  • I want my editext should not take more than 10 character...if it takes it showa some error – Pranesh Sahu Jul 06 '17 at 16:02
  • Possible duplicate of [Edit text Max length and show the length in the texview](https://stackoverflow.com/questions/21718733/edit-text-max-length-and-show-the-length-in-the-texview) – Sufian Jul 06 '17 at 16:07

1 Answers1

1

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.

Robert Estivill
  • 12,369
  • 8
  • 43
  • 64
  • The error msg will display if edittext value is greater than 10 – Pranesh Sahu Jul 06 '17 at 15:42
  • Are you creating two different instances TextWatchers? one per EditText ? There's something weird there. You are calling txtEmail and txtFullName from inside the TextWatcher without being passed in as parameters or constructor. PLease paste all the code – Robert Estivill Jul 06 '17 at 15:54
  • 1
    So your logic is only based on one of the views, because you are doing `switch (view.getId())` this will always be the same view. – Robert Estivill Jul 06 '17 at 16:02
  • What happen is after entering 10 character the error msg comes in & when i pressed another character the textwatcher get invoked but if i pressed "space" the textwatcher is not getting called & the error message disappears – Pranesh Sahu Jul 06 '17 at 16:09
  • Is NOT using the same TextWatcher, you just added that code on top. There are two different instances of TextWatcher – Robert Estivill Jul 06 '17 at 16:27