Search code examples
androidlayouticonsandroid-textinputlayout

Error drawable and color don't change in textInputLayout


So I'm using a textInputLayout and all I need to do is set a custom drawable for the error icon. Here's my layout and dependency

    implementation 'com.google.android.material:material:1.2.0-beta01'
    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="@dimen/text_field_height"
        android:layout_margin="@dimen/activity_margin"
        android:hint="@string/insert_name_hint"
        app:boxBackgroundColor="@color/transparent"
        app:boxCornerRadiusBottomEnd="@dimen/text_field_corner"
        app:boxCornerRadiusBottomStart="@dimen/text_field_corner"
        app:boxCornerRadiusTopEnd="@dimen/text_field_corner"
        app:boxCornerRadiusTopStart="@dimen/text_field_corner"
        app:errorEnabled="true"
        app:errorTextColor="?colorAccent"
        app:errorIconDrawable="@drawable/ic_alert_black_24dp"
        app:errorIconTint="?colorAccent"
        app:errorContentDescription="@string/insert_name_hint"
        app:boxStrokeErrorColor="@color/gray"
        app:startIconDrawable="@drawable/ic_cool_black_24dp"
        app:startIconTint="@color/gray"
        app:hintAnimationEnabled="true"
        app:hintTextColor="?colorAccent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/nameEvent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:imeOptions="actionDone"
            android:maxLength="30"
            android:padding="0dp"
            android:textAlignment="center"
            android:textCursorDrawable="@null"
            android:inputType="textCapSentences"/>

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

Basically, the drawable and the tints don't work, the default icon and color are used. The rest works as it should. I'm not setting anything programmatically.


Solution

  • Well, the problem was kinda basic, but i found out after a while. It's not a layout-level problem, but an error management problem. The error must be set on the Input Layout and not on the Input EditText. So here's the difference (i also had to add an id to the Input Layout of course):

    Before

    customView.nameEvent.error = getString(R.string.invalid_value_name)
    

    After

    customView.nameEventLayout.error = getString(R.string.invalid_value_name)
    

    Yep, is that simple. But i think this could be a common error, so i hope this answer can help somebody out there.