how do I display the alternating icons in editText?

hashtag :

so I have editText with closes and success icons and maximum password 6, the first thing I do is if the password is not maximum 6, the icon remains closes like I drew, if the password is maximum 6 then the closes icon changes to the success icon, my problem how to change the closes icon to a success icon if the maximum 6 conditions are met

editTest image

this is my code

 <com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputPassword"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="20dp"
    android:hint="Password"
    app:counterEnabled="true"
    app:counterMaxLength="6"
    app:endIconMode="clear_text"
    app:endIconDrawable="@drawable/ic_canceles"
    app:errorEnabled="true"
    app:endIconTint="#DF0000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputEmail">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/editTextPassword"
        android:layout_width="match_parent"
        android:maxLines="1"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>
NattoShaw :

Try this in your on create method

TextInputLayout editText = findViewById(R.id.textInputPassword);


                editText.getEditText().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.toString().length() > 6) {
                            editText.setEndIconDrawable(R.drawable.success);
                        } else {
                            editText.setEndIconDrawable(R.drawable.ic_canceles);
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400291&siteId=1