Change underline color of edittext if it is filled with input?

Afdal Dev :

Change underline color of edittext if it is filled with input ?

enter image description here

State :

  1. unfocused : color/1
  2. focused : color/2
  3. unfocused(after input) : color/2

i've try use :

edtText.doAfterTextChanged {

}

but, i can't find attribut "edtText.backgroundTint" in doAfterTextChanged

Vishist Varugeese :

You can use TextWatcher for this and setBackgroundTintList to change the line color,

et.addTextChangedListener(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) {
            if (s != null && !(s.toString().isEmpty())) {
                //Color when text exists
                ColorStateList colorStateList = ColorStateList.valueOf(getResources().getColor(R.color.colorPrimaryDark));
                ViewCompat.setBackgroundTintList(et, colorStateList);
            } else {
                //Default color - color when text does not exist
                ColorStateList colorStateList = ColorStateList.valueOf(getResources().getColor(R.color.colorAccent));
                ViewCompat.setBackgroundTintList(et, colorStateList);
            }
        }
        @Override
        public void afterTextChanged(Editable s) {}
    });

Please refer to this link.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=350450&siteId=1