We can manipulate the error as helper text because if you set an error it will definitely change the hint color, so I just used helperText
instead of setError
, here is an example
XML File
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayoutUserName"
android:layout_width="@dimen/view_0x"
android:layout_height="wrap_content"
style="@style/errorEnabledtextInputLayoutStyle">
<EditText
android:id="@+id/editTextUserName"
android:gravity="center_vertical"
android:background="@drawable/edit_text_background"
android:backgroundTint="@color/colorGray"
android:hint="@string/aap_ka_naam"
android:singleLine="true"
style="@style/standardTextViewStyle"
tools:targetApi="lollipop"
app:layout_constraintHorizontal_bias="0.0"/>
</com.google.android.material.textfield.TextInputLayout>
edit_text_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-3dp" android:left="-3dp" android:right="-3dp">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="@color/colorWhite" />
<padding android:bottom="50dp" android:top="10dp" android:right="10dp"android:left="5dp"/>
<solid android:color="#00000000" />
</shape>
</item>
</layer-list>
Styles
<style name="errorEnabledtextInputLayoutStyle">
<item name="hintTextColor">@color/colorGraySix</item>
<item name="helperTextTextColor">@color/colorNegative</item>
<item name="hintEnabled">true</item>
</style>
Function
fun TextInputLayout.setErrorText(error : String, editText: EditText) {
this.helperText = error
editText.backgroundTintList = ColorStateList.valueOf(
ResourcesCompat.getColor(resources,
R.color.colorNegative, null)
)
}
Usage
textInputLayoutUserName.setErrorText(getString(R.string.name_error), editTextUserName)