Set Error in Password Field but keep the password_toggle

Viewed 19

How can I keep the password_toggle when Im using setError() in a TextInputLayout? I tried by using this in code:

textInputLayoutPasswordNewUser.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);

XML CODE:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayoutPasswordNewUser"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="327dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="32dp"
    android:hint="@string/password"
    app:errorEnabled="true"
    app:endIconMode="password_toggle"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:startIconDrawable="@android:drawable/ic_lock_idle_lock">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/txtPasswordNewUser"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

This is my validation class:

public static boolean validatePassword(TextInputLayout textInputLayoutPasswordNewUser, EditText txtPasswordNewUser){

    boolean datosCorrectos = true;
    textInputLayoutPasswordNewUser.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);


    if(txtPasswordNewUser.getText().toString().trim().length() != 0){

        if(!txtPasswordNewUser.getText().toString().matches("^(?=.*\\d)(?=.*[a-zA-Z]).{8,12}$")){

            textInputLayoutPasswordNewUser.setError(textInputLayoutPasswordNewUser.getContext().getResources().getString(R.string.password_requires));
            textInputLayoutPasswordNewUser.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
            datosCorrectos = false;
        }

    }else{

        textInputLayoutPasswordNewUser.setError(textInputLayoutPasswordNewUser.getContext().getResources().getString(R.string.empty_password));
        textInputLayoutPasswordNewUser.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
        datosCorrectos = false;
    }

    if(datosCorrectos){

        textInputLayoutPasswordNewUser.setError(null);
        textInputLayoutPasswordNewUser.setBoxStrokeColor(ContextCompat.getColor(textInputLayoutPasswordNewUser.getContext(), R.color.green_correct));
        textInputLayoutPasswordNewUser.setHelperText(textInputLayoutPasswordNewUser.getContext().getResources().getString(R.string.correct_password));

    }

    return datosCorrectos;
}
0 Answers
Related