Egor Egor - 1 month ago 13
Android Question

EditText maximum characters limit exceeded callback

My idea is to set an error

View
to the
EditText
when the maximum character limit has been reached. Is there any callback about this event, or may be there's another way to achieve this effect? Thanks in advance.

Answer

You can use the edit text's setError:

editText.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) {
            if(s.length() > max)
                editText.setError("Error");
        }
    });
Comments