Search code examples
androidmaterial-designandroid-textinputlayoutmaterial-components-androidmaterial-components

customize Bounding box stroke in TextInputLayout.OutlinedBox


I'm trying to change box Stroke Color of com.google.android.material.textfield.TextInputLayout while not focused but there is no attribute for that this is my TextInputLayout code

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="36dp"
    android:layout_marginTop="38dp"
    android:layout_marginEnd="36dp"
    android:textColorHint="@color/white"
    app:boxStrokeColor="@color/white"
    app:boxStrokeWidth="1dp">

    <EditText
        android:id="@+id/editTxt_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_email"
        android:inputType="textEmailAddress"
        android:textColorHint="@color/white" />
</com.google.android.material.textfield.TextInputLayout>

and I searched for a way to change its color and found this link : https://github.com/material-components/material-components-android/issues/112

so i tried using this line in my colors file

<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">#fff</color>

this solved the problem and changed stroke box color but the issue here is that i want to change this color in other TextInputLayouts in the same app !!


Solution

  • To change the box stroke color just use the app:boxStrokeColor attribute in the xml.

     <com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            app:boxStrokeColor="@color/mySelector"
            ../>
    

    You should use a selector. It is the default:

    <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 also use a custom style:

      <style name="MyOutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="boxStrokeColor">@color/text_input_selector</item>
      </style>
    

    enter image description here enter image description here