1

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Fried Rice
  • 3,295
  • 3
  • 18
  • 27

2 Answers2

4

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

hmm, see if you use directly below

style="@style/Widget.Design.TextInputLayout"

then your error drawable becomes null.

What I would suggest you is, create a style, modify the values as you like

<style name="TextLabel" parent="Widget.Design.TextInputLayout">
    <item name="android:textColorHint">#f32</item>
    <item name="android:textSize">20sp</item>
    <item name="colorAccent">#4CAF50</item>
    <item name="errorIconDrawable">@drawable/ic_baseline_error_24</item>
    <item name="colorControlNormal">#673AB7</item>
    <item name="colorControlActivated">#E91E63</item>
    <item name="errorEnabled">true</item>
</style>

and then apply it to via

   <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_password_input_layout"
        android:theme="@style/TextLabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        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>

This should solve.

hemen
  • 1,460
  • 2
  • 16
  • 35