Search code examples
androidlayoutstylesandroid-textinputlayout

boxStrokeColor with com.google.android.material.textfield.TextInputLayout is not work


I use TextInputLayout in my application and I want to change the boxStroke color. I use app: boxStrokeColor = "@ color / white" to change the Box Stroke color, but the color of the BoxStrokeColor does not change.

But when I add the following code block into color.xml, when I use it in the application, it affects the boxStrokeColor of all TextInputLayout's in the application.

<colorname="mtrl_textinput_default_box_stroke_color"tools:override="true">@color/white How can I fix that problem? related code block

<com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColorHint="@color/LightBlue"
            android:theme="@style/ThemeOverlay.AppTheme.TextInputEditText.Outlined"
            app:boxStrokeColor="@color/white"
            app:boxStrokeWidth="3dp"
            app:startIconTint="@color/white">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/numberOfPlayer_txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:textColor="@color/gold"


                />

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

in Style :

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
</style>


<style name="AppBaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
        <item name="android:windowBackground">@color/Thistle</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

<style name="ThemeOverlay.AppTheme.TextInputEditText.Outlined" parent="">
        <item name="colorPrimary">@color/white</item>
        <item name="boxCornerRadiusBottomEnd">20dp</item>
        <item name="boxCornerRadiusBottomStart">20dp</item>
        <item name="boxCornerRadiusTopEnd">20dp</item>
        <item name="boxCornerRadiusTopStart">20dp</item>
        <item name="android:hint">Number of player</item>
        <item name="hintTextColor">@color/card1</item>
        <item name="startIconDrawable">@drawable/ic_people</item>

    </style>

implementation 'com.google.android.material:material:1.1.0'

Solution

  • If attribute boxStrokeColor is not a color state list but only a single value, its value will be applied to the box's focus state.

    Instead of a single color just define a selector:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/white" android:state_focused="true"/>
        <item android:color="@color/...." android:state_hovered="true"/>
        <item android:color="@color/...." android:state_enabled="false"/>
        <item android:color="@color/...."/>
    </selector>
    

    and:

    <com.google.android.material.textfield.TextInputLayout
           app:boxStrokeColor="@color/selector"
           ...>