Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout removes EditText style after setting setError property to null

Tags:

android

I want to use TextInputLayout with my new app. I have such layout

***
    <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:textColorHint="@color/text_color"
        app:hintTextAppearance="@style/HintTextAppearance.TextInputLayout"
        app:errorTextAppearance="@style/ErrorTextAppearance.TextInputLayout">

        <EditText
            android:id="@+id/input_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="@string/hint_email"
            android:background="@drawable/edit_text_border_radius"
            android:padding="10dp"
            android:drawableLeft="@drawable/ic_acc"/>
    </android.support.design.widget.TextInputLayout>
 ***

On my activity i have validation as below:

    private boolean validatePassword() {
            if (inputPassword.getText().toString().trim().isEmpty()) {
                inputLayoutPassword.setError(getString(R.string.err_msg_password));
                requestFocus(inputPassword);
                return false;
            } else {
                inputLayoutPassword.setError(null);// it removes @drawable/edit_text_border_radius style from EditText
                inputLayoutPassword.setErrorEnabled(false);      
            }

            return true;
     }

Not it works correctly. but as if you notices i have declared @drawable/edit_text_border_radius resource for EditText. And if first time i do not fill password field it is going to change it is background color to red. As it is default color for TextInputLayout error span. But then if i fill same field with some values then red error span disappears but EditText element forget it is background resource(@drawable/edit_text_border_radius) set to it before.

like image 698
AEMLoviji Avatar asked Oct 31 '22 18:10

AEMLoviji


1 Answers

Not sure if you've found a solution to your problem, but I just ran into the same issue.

Digging into the TextInputLayout source, especially the logic around clearing the error message, it looks like the EditText gets it's background color filter cleared (in my case, it was turning black).

The quick and dirty solution I've come up with in the mean time is to just manually reset the background filter to the desired color:

private boolean validatePassword() {
    if (inputPassword.getText().toString().trim().isEmpty()) {
        inputLayoutPassword.setError(getString(R.string.err_msg_password));
        requestFocus(inputPassword);
        return false;
    } else {
        inputLayoutPassword.setError(null);// it removes @drawable/edit_text_border_radius style from EditText
        inputLayoutPassword.setErrorEnabled(false);

        // manually resetting the background color filter of edit text
        if(inputLayoutPassword.getEditText() != null) {
            if(inputLayoutPassword.getEditText().getBackground() != null) {
                inputLayoutPassword.getEditText()
                    .getBackground()
                    .setColorFilter(
                        ContextCompat.getColor(getActivity(), R.color.some_color),
                        PorterDuff.Mode.SRC_IN
                    );
            }
        }
    }

    return true;
}
like image 81
Brad Avatar answered Nov 15 '22 05:11

Brad