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
Arranging the accepted solution with https://stackoverflow.com/a/40379564/2914140 and https://stackoverflow.com/a/44744941/2914140, I wrote another class. When an EditText has a special background (background_1) it changes to a background_2 on error. When the error disappears, it again returns to background_1. No red fill is executed.
public class YourTextInputLayout extends TextInputLayout {
private Drawable drawable1; // Normal background.
private Drawable drawable2; // Error background.
public YourTextInputLayout(Context context) {
super(context);
}
public YourTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public YourTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
replaceBackground();
}
@Override
public void setError(@Nullable final CharSequence error) {
super.setError(error);
replaceBackground();
}
public void setDrawable1(Drawable drawable) {
this.drawable1 = drawable;
}
public void setDrawable2(Drawable drawable) {
this.drawable2 = drawable;
}
private void replaceBackground() {
EditText editText = getEditText();
if (editText != null) {
editText.setBackground(isErrorEnabled() ? drawable2 : drawable1);
Drawable drawable = editText.getBackground();
if (drawable != null) {
drawable.clearColorFilter();
}
}
}
}
In your activity/fragment call after initialization in onCreate() / onCreateView():
YourTextInputLayout inputLayout = ...;
inputLayout.setDrawable1(ContextCompat.getDrawable(getContext(), R.drawable.background_1));
inputLayout.setDrawable2(ContextCompat.getDrawable(getContext(), R.drawable.background_2));
In your XML layout call it by: