Issue in Changing EditText background after setting error to the TextInputLayout on Android M
You can achieve this using custom Edittext background.
Create selector drawable like below,
Then,
edittext_normal.xml
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="@color/edit_text_normal_color" />
</shape>
</item>
edittext_focused.xml
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="@color/colorPrimary" />
</shape>
</item>
edittext_red_focused.xml
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="@color/sale_color" />
</shape>
</item>
And set the error drawable at runtime, when error occurs
Validation Code
private boolean validateName() {
if (inputName.getText().toString().trim().isEmpty()) {
inputLayoutName.setErrorEnabled(true);
inputLayoutName.setError("Error");
inputName.setBackgroundResource(R.drawable.edittext_red_focused);
return false;
} else {
inputName.setBackgroundResource(R.drawable.edittext_selector);
inputLayoutName.setErrorEnabled(false);
}
return true;
}
Do not change TextInputLayout background on error
I manage to resolve this myself by overriding TextInputLayout like this :
public class NoChangingBackgroundTextInputLayout extends TextInputLayout {
public NoChangingBackgroundTextInputLayout(Context context) {
super(context);
}
public NoChangingBackgroundTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NoChangingBackgroundTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setError(@Nullable CharSequence error) {
ColorFilter defaultColorFilter = getBackgroundDefaultColorFilter();
super.setError(error);
//Reset EditText's background color to default.
updateBackgroundColorFilter(defaultColorFilter);
}
@Override
protected void drawableStateChanged() {
ColorFilter defaultColorFilter = getBackgroundDefaultColorFilter();
super.drawableStateChanged();
//Reset EditText's background color to default.
updateBackgroundColorFilter(defaultColorFilter);
}
private void updateBackgroundColorFilter(ColorFilter colorFilter) {
if(getEditText() != null && getEditText().getBackground() != null)
getEditText().getBackground().setColorFilter(colorFilter);
}
@Nullable
private ColorFilter getBackgroundDefaultColorFilter() {
ColorFilter defaultColorFilter = null;
if(getEditText() != null && getEditText().getBackground() != null)
defaultColorFilter = DrawableCompat.getColorFilter(getEditText().getBackground());
return defaultColorFilter;
}
}
So as we can see it, it reset the EditText's background to its default color after setError has been called but also in the method drawableStateChanged()
because the red color filter is set when losing/getting the focus on an EditText with error too.
I'm not convinced that this is the best solution but if I don't get any better solutions, I'll mark it as resolved in meantime.
How to change an edittext border colour to red on error?
Create two seprate background like this
error background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#ff00" />
<corners android:radius="5dp" />
</shape>
normal background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke
android:width="1dp"
android:color="#0059ff" />
<corners android:radius="5dp" />
</shape>
Try this
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(edittext.getText().toString().isEmpty()){
edittext.setError("error");
edittext.setBackgroundResource(R.drawable.edterr);
}else {
edittext.setBackgroundResource(R.drawable.edtnormal);
}
}
});
Related Topics
How to Set The App Icon as The Notification Icon in The Notification Drawer
Android Alarmmanager Setexact() Is Not Exact
Service Intent Must Be Explicit: Intent
How to Pause Android.Speech.Tts.Texttospeech
How to Get a View from an Event Coordinates in Android
Android View Layout_Width - How to Change Programmatically
Android: Radio Button in Custom List View
Adding a Vertical Scrollbar to an Alertdialog in Android
Activitymanager.Getrunningtasks Is Deprecated Android
How to Be Notified When a Snackbar Has Dismissed Itself
How to Stop a Service Running as Foreground
Asynctaskloader Basic Example. (Android)
Import an Eclipse Android Project with Version Control System into Android Studio
Retrofit with Rxjava Handling Network Exceptions Globally