1

I'm using com.google.android.material:material:1.1.0 on Android 9.0. I want to set the underline color of TextInputLayout when it's not focused but nothing works.

My style:

<style name="EditTextTheme" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <item name="colorAccent">#00BFFF</item>
    <item name="colorControlNormal">#FFFF00</item>
    <item name="colorControlActivated">#FF00FF</item>
    <item name="colorControlHighlight">#FFFF00</item>
    <item name="boxStrokeColor">@color/text_input_box_stroke</item>
</style>

My selector(text_input_box_stroke):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#00BFFF" android:state_focused="true"/>
    <item android:color="#FFFF00" android:state_hovered="true"/>
    <item android:color="#00FF00" android:state_enabled="false"/>
</selector>

And my TextInputLayout:

<com.google.android.material.textfield.TextInputLayout
    style="@style/EditTextTheme"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:hint="Email">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textWebEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
Edric
  • 24,639
  • 13
  • 81
  • 91
scof93
  • 359
  • 1
  • 4
  • 14

1 Answers1

1

right-click res -> new -> Android Resource Directory -> Resource type -> color name your file for example text_input_box_stroke.xml and put it in color folder

text_input_box_stroke.xml

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:state_enabled="true"
    android:color="#00BFFF" />
  <item
    android:state_pressed="true"
    android:color="#FFFF00" />
  <item
    android:state_focused="true"
    android:color="#00C853" />
 <item
    android:color="#ff00ff" />
</selector>

to prevent TextInputLayout view from gaining focus when activity or fragment which contains it start put this attributes to the parent layout which contains TextInputLayout

android:fadeScrollbars="false"
android:fillViewport="true"

layout file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="@+id/content">
    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/layout_data"
        style="@style/TextInputLayoutStyleFilledBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Hint"
        android:textColorHint="@color/black"
        app:boxBackgroundColor="@android:color/transparent"
        app:boxStrokeWidth="2dp"
        app:hintTextColor="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/input_value"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="123456789" />
    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

styles.xml

<style name="TextInputLayoutStyleFilledBox" 
 parent="@style/Widget.MaterialComponents.TextInputLayout.FilledBox">
    <item name="colorAccent">#00BFFF</item>
    <item name="colorControlNormal">#FFFF00</item>
    <item name="colorControlActivated">#FF00FF</item>
    <item name="colorControlHighlight">#FFFF00</item>
    <item name="boxStrokeColor">@color/text_input_box_stroke</item>
</style>
Mohammed Alaa
  • 3,140
  • 2
  • 19
  • 21