I would like to have an EditText with the background as a "normal" EditText but with the error handling of a TextInputEditText (error message appearing at the bottom, and not the "!" drawable appearing).
I got something like this :
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setError="@{viewModel.error}">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/simple_edit_text_background"
android:ellipsize="end"
android:inputType="textMultiLine|textNoSuggestions"
android:text="@={viewModel.value}"
style="@style/MyEditTextStyle" />
</android.support.design.widget.TextInputLayout>
But it seems that when I set error on the TextInputLayout it changes the background drawable(which is, in normal TextInputEditText, the underline) to the color of the error TextView.
And so this is how my EditText looks like :
We can see it in the code of TextInputLayout within the following method :
private void updateEditTextBackground() {
if (mEditText == null) {
return;
}
Drawable editTextBackground = mEditText.getBackground();
if (editTextBackground == null) {
return;
}
ensureBackgroundDrawableStateWorkaround();
if (android.support.v7.widget.DrawableUtils.canSafelyMutateDrawable(editTextBackground)) {
editTextBackground = editTextBackground.mutate();
}
if (mErrorShown && mErrorView != null) {
// Set a color filter of the error color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
mErrorView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
} else if (mCounterOverflowed && mCounterView != null) {
// Set a color filter of the counter color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
mCounterView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
} else {
// Else reset the color filter and refresh the drawable state so that the
// normal tint is used
DrawableCompat.clearColorFilter(editTextBackground);
mEditText.refreshDrawableState();
}
}
The code's block that update the backgroud color is here :
if (mErrorShown && mErrorView != null) {
// Set a color filter of the error color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
mErrorView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
}
Because this method is private I can't override it and because I still wants my error TextView's color to be red I can't see any solution so far. Any idea?
One solution could maybe to reset background color to its default value just after setError
would have been called but is their any callback with a method like onError
that will be fired once an error is set to a TextView/EditText?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…