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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With