-1

I am trying to validate some EditText with a TextWathcher as follows:

TextWatcher textWatcher = new TextWatcher() {
            boolean ServerOK = false;
            boolean NameOK = false;
            boolean EmpidOK = false;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                String txtName = txtEmployeeName.getText().toString();
                String txtEmpid = txtEmployeeID.getText().toString();
                if(URLUtil.isValidUrl(txtServerName.getText().toString()))
                {
                    ServerOK = true;
                }

                // Name input is fine
                if(txtName.length() > -1) {
                    NameOK = true;
                }

                // Employee ID input is fine
                if(txtEmpid.length() > -1) {
                    EmpidOK = true;
                }

                // Validate both fields and activate button if all OK
                if (NameOK && ServerOK && EmpidOK) {
                    btnRegister.setEnabled(true);
                } else {
                    btnRegister.setEnabled(false);
                }
                System.out.println(NameOK + " " + EmpidOK + " " + ServerOK);
            }
        };
        txtEmployeeName.addTextChangedListener(textWatcher);
        txtServerName.addTextChangedListener(textWatcher);
        txtEmployeeID.addTextChangedListener(textWatcher);
 }

The issue is that when typing in some text in the EdiText the booleans are set correctly. If I delete any of the strings in the TexViews as soon as I hit 0 characters in the EditText:

E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
    SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

What could be the cause of this error?

Fabrizio Mazzoni
  • 1,831
  • 2
  • 24
  • 46

3 Answers3

1

try this

TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }
        @Override
        public void afterTextChanged(Editable s) {
            btnRegister.setEnabled(!txtEmployeeName.getText().toString().isEmpty() && !txtEmployeeID.getText().toString().isEmpty()&&URLUtil.isValidUrl(txtServerName.getText().toString()));
        }
    };

don't forgot to add btnRegister.setEnabled(false); in on create

mitesh makwana
  • 269
  • 3
  • 11
0
android:inputType="textNoSuggestions"

Try to set above property inside the Edit text view along with using a default keyboard.

Rudrik Patel
  • 676
  • 6
  • 13
0

I am using this library: https://github.com/pawegio/KAndroid

example:

passwordEditText.textWatcher {
            afterTextChanged {
                ...
            }
        }

It is very simple to use

Karim
  • 962
  • 7
  • 10