androidmaterial-components-androidandroid-textinputlayouttextinputlayout

TextInputLayout will not set errorIconDrawable


When I use style="@style/Widget.Design.TextInputLayout" for TextInputLayout, setting app:errorIconDrawable does nothing. It only works when I don't set the style and let it inherit the application theme (Theme.MaterialComponents.Light.NoActionBar). app:endIconDrawable also doesn't work and I cannot find an alternative/solution to this problem. Please help!


The following works while inheriting the application theme:Theme.MaterialComponents.Light.NoActionBar but I don't want this style, particularly the underline doesn't align with the texts:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/login_password_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="username"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/login_password_input_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

enter image description here


This uses the style that I need but will not show the error icon:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/login_password_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="username"
    style="@style/Widget.Design.TextInputLayout"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/login_password_input_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

enter image description here


Solution

  • Since you are using the legacy style Widget.Design.TextInputLayout it doesn't have support for all text field features that the *.OutlinedBox and *.FilledBox styles have.

    You can't set the errorIconDrawable in the xml and you have to call TextInputLayout.setErrorIconDrawable after TextInputLayout.setError.

    login_password_input_layout.setError("Error text!!");                
    login_password_input_layout.setErrorIconDrawable(R.drawable....);
    

    enter image description here