TextInputLayout removes the EditText style after setting the setError property to null

I want to use TextInputLayout with my new application. I have such a 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> *** 

In my activity I have confirmation, as shown 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; } 

Does not work. but as you noticed, I declared the @ drawable / edit_text_border_radius resource for EditText. And if for the first time I do not fill out the password field, it will change the background color to red. Since this is the default color for the TextInputLayout error range. But then, if I fill in the same field with some values, then the red error range disappears, but the EditText element forgets that the background resource (@ drawable / edit_text_border_radius) is set in front of it.

+5
source share
1 answer

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

Digging into the source TextInputLayout , especially the logic for clearing the error message, it seems that EditText will clear the background color filter (in my case, it turned black).

The quick and dirty solution I came up with is simply manually resetting 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; } 
+2
source

Source: https://habr.com/ru/post/1232523/


All Articles