I have set custom background to the EditText in Xml file. After validation, i am setting different background to EditText at runtime and also setting error to TextInputLayout. But in Android M instead of setting background resource, it is setting background color to EditText.
This is image before setting error to the TextInputLayout
This is the image after setting error to the TextInputLayout
Below is my code:-
XML file code
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:errorTextAppearance="@style/ErrorText">
<EditText
android:id="@+id/input_name"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/edittext_selector"
android:hint="@string/hint_name"
android:imeOptions="actionNext"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
Validation code
if (inputName.getText().toString().trim().isEmpty()) {
inputLayoutName.setErrorEnabled(true);
inputLayoutName.setError("Errorrroror");
inputName.setBackgroundResource(R.drawable.edittext_red_focused);
requestFocus(inputName);
return false;
} else {
inputName.setBackgroundResource(R.drawable.edittext_selector);
inputLayoutName.setErrorEnabled(false);
}
return true;
Below is my resource file for error
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="@color/sale_color" />
</shape>
</item>
<!-- main color -->
<item
android:bottom="1.0dp"
android:left="1.0dp"
android:right="1.0dp">
<shape>
<solid android:color="@android:color/white" />
</shape>
</item>
<!-- draw another block to cut-off the left and right bars -->
<item android:bottom="1.0dp">
<shape>
<solid android:color="@android:color/white" />
</shape>
</item>
</layer-list>
You can achieve this using custom Edittext background.
Create selector drawable like below,
Then,
edittext_normal.xml
edittext_focused.xml
edittext_red_focused.xml
And set the error drawable at runtime, when error occurs
Validation Code