1

I am trying to set the underline color of a TextInputEditText in a TextInputLayout. When setting colorControlActivated to the desired color, the cursor changes color but the underline does not respond.

I have tried:

  • Creating a style with colorControlHighlight, colorControlNormal, and colorControlActivated defined. Setting widget themes both in the layout file and in the manifest.
  • Setting the background of the input field to a drawable with the underline manually drawn in.

Here is the style defined in values/themes.xml:

<style name="AppTheme.DetailItem" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="android:colorControlNormal">#ff0000</item>
    <item name="android:colorControlHighlight">#0000ff</item>
    <item name="android:colorControlActivated">#ff00ff</item>
</style>

And the theme applied:

<com.google.android.material.textfield.TextInputLayout
    android:theme="@style/AppTheme.DetailItem"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:errorEnabled="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:enabled="false"
        android:maxLines="1"
        android:textAppearance="?textAppearanceBody2"
        tools:text="Name" />
</com.google.android.material.textfield.TextInputLayout>

I would expect the underline of the TextInputEditText to change color to colorControlActivated when focused, and colorControlNormal when not focused. However, no matter what, the underline color remains black when not focused, and colorPrimary when focused. However, the cursor does change to colorControlActivated when the field is focused, so the widget is recognizing the style I have defined, but it is not using the style colors to draw the underline.

canons
  • 11
  • 4

1 Answers1

0

Since you are using a FilledBox the underline color is defined by boxStrokeColor attribute.

It is the default selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_focused="true"/>
  <item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>

You can set a custom selector or you can override the colorOnSurface and colorPrimary color with

<com.google.android.material.textfield.TextInputLayout
    android:theme="@style/AppTheme.DetailItem"
    ...>

where:

  <style name="AppTheme.DetailItem">
    <item name="colorOnSurface">@color/...</item>
    <item name="colorPrimary">@color/....</item>
    ....
  </style>

enter image description hereenter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Thanks for your response. When setting `boxStrokeColor` to a custom selector or even just a color, I am not seeing a change. I do see a change when using `colorOnSurface`, however this also changes the label color. – canons Oct 16 '19 at 17:21
  • It seems like I am unable to interact with any of the `TextInputLayout` color theming attributes. The only times I have seen change related to the `TextInputLayout` colors is when setting a color such as `colorOnSurface` which the default theming attributes pull from (like in the `boxStrokeColor` default selector you mentioned) – canons Oct 16 '19 at 17:26
  • Are you using the version 1.1.0-beta01? – Gabriele Mariotti Oct 16 '19 at 18:17
  • `1.1.0-alpha06` – canons Oct 16 '19 at 18:41
  • After updating to `1.1.0-beta01`, I am still seeing the problem – canons Oct 16 '19 at 19:11
  • Quite strange. Using the attributes in the layout you shouldn't have issue with theme or colors. – Gabriele Mariotti Oct 16 '19 at 19:32
  • Agreed, strange. Thanks for your help. I'll update if I ever figure out what's wrong. – canons Oct 16 '19 at 19:44