How to change the floating label color of TextInputLayout

前端 未结 25 1750
[愿得一人]
[愿得一人] 2020-11-22 15:03

With reference to the new TextInputLayout released by Google, how do I change the floating label text color?

Setting colorControlNormal,

相关标签:
25条回答
  • 2020-11-22 15:21

    Try The Below Code It Works In Normal State

     <android.support.design.widget.TextInputLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:theme="@style/TextLabel">
    
         <android.support.v7.widget.AppCompatEditText
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:hint="Hiiiii"
             android:id="@+id/edit_id"/>
    
     </android.support.design.widget.TextInputLayout>
    

    In Styles Folder TextLabel Code

     <style name="TextLabel" parent="TextAppearance.AppCompat">
        <!-- Hint color and label color in FALSE state -->
        <item name="android:textColorHint">@color/Color Name</item> 
        <item name="android:textSize">20sp</item>
        <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
        <item name="colorAccent">@color/Color Name</item>
        <item name="colorControlNormal">@color/Color Name</item>
        <item name="colorControlActivated">@color/Color Name</item>
     </style>
    

    Set To Main Theme of App,It Works Only Highlight State Only

     <item name="colorAccent">@color/Color Name</item>
    

    Update:

    UnsupportedOperationException: Can't convert to color: type=0x2 in api 16 or below

    Solution

    Update:

    Are you using Material Components Library

    You can add below lines to your main theme

     <item name="colorPrimary">@color/your_color</item> // Activated State
     <item name="colorOnSurface">@color/your_color</item> // Normal State
    

    or else do you want different colors in noraml state and activated state and with customization follow below code

    <style name="Widget.App.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.TextInputLayout</item>
        <item name="shapeAppearance">@style/ShapeAppearance.App.SmallComponent</item> //Changes the Shape Apperance
        <!--<item name="hintTextColor">?attr/colorOnSurface</item>-->   //When you added this line it will applies only one color in normal and activate state i.e colorOnSurface color
    </style>
    
    <style name="ThemeOverlay.App.TextInputLayout" parent="">
        <item name="colorPrimary">@color/colorPrimaryDark</item>  //Activated color
        <item name="colorOnSurface">@color/colorPrimary</item>    //Normal color
        <item name="colorError">@color/colorPrimary</item>        //Error color
    
        //Text Appearance styles
        <item name="textAppearanceSubtitle1">@style/TextAppearance.App.Subtitle1</item>
        <item name="textAppearanceCaption">@style/TextAppearance.App.Caption</item>
    
        <!--Note: When setting a materialThemeOverlay on a custom TextInputLayout style, don’t forget to set editTextStyle to either a @style/Widget.MaterialComponents.TextInputEditText.* style or to a custom one that inherits from that.
        The TextInputLayout styles set materialThemeOverlay that overrides editTextStyle with the specific TextInputEditText style needed. Therefore, you don’t need to specify a style tag on the edit text.-->
        <item name="editTextStyle">@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox</item>
    </style>
    
    <style name="TextAppearance.App.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
        <item name="fontFamily">@font/your_font</item>
        <item name="android:fontFamily">@font/your_font</item>
    </style>
    
    <style name="TextAppearance.App.Caption" parent="TextAppearance.MaterialComponents.Caption">
        <item name="fontFamily">@font/your_font</item>
        <item name="android:fontFamily">@font/your_font</item>
    </style>
    
    <style name="ShapeAppearance.App.SmallComponent" parent="ShapeAppearance.MaterialComponents.SmallComponent">
        <item name="cornerFamily">cut</item>
        <item name="cornerSize">4dp</item>
    </style>
    

    Add the below line to your main theme or else you can set style to textinputlayout in your xml

    <item name="textInputStyle">@style/Widget.App.TextInputLayout</item>
    
    0 讨论(0)
  • 2020-11-22 15:21

    Programmatically you can use:

    /* Here you get int representation of an HTML color resources */
    int yourColorWhenEnabled = ContextCompat.getColor(getContext(), R.color.your_color_enabled);
    int yourColorWhenDisabled = ContextCompat.getColor(getContext(), R.color.your_color_disabled);
    
    /* Here you get matrix of states, I suppose it is a matrix because using a matrix you can set the same color (you have an array of colors) for different states in the same array */
    int[][] states = new int[][]{new int[]{android.R.attr.state_enabled}, new int[]{-android.R.attr.state_enabled}};
    
    /* You pass a ColorStateList instance to "setDefaultHintTextColor" method, remember that you have a matrix for the states of the view and an array for the colors. So the color in position "colors[0x0]" will be used for every states inside the array in the same position inside the matrix "states", so in the array "states[0x0]". So you have "colors[pos] -> states[pos]", or "colors[pos] -> color used for every states inside the array of view states -> states[pos] */
    myTextInputLayout.setDefaultHintTextColor(new ColorStateList(states, new int[]{yourColorWhenEnabled, yourColorWhenDisabled})
    

    Explaination:

    Get int color value from a color resource (a way to present rgb colors used by android). I wrote ColorEnabled, but really it should be, for this answer, ColorHintExpanded & ColorViewCollapsed. Anyway this is the color you will see when the hint of a view "TextInputLayout" is on Expanded or Collapsed state; you will set it by using next array on function "setDefaultHintTextColor" of the view. Reference: Reference for TextInputLayout - search in this page the method "setDefaultHintTextColor" for more info

    Looking to docs above you can see that the functions set the colors for Expanded & Collapsed hint by using a ColorStateList.

    ColorStateList docs

    To create the ColorStateList I first created a matrix with the states I want, in my case state_enabled & state_disabled (whose are, in TextInputLayout, equals to Hint Expanded and Hint Collapsed [I don't remember in which order lol, anyway I found it just doing a test]). Then I pass to the constructor of the ColorStateList the arrays with int values of color resources, these colors have a correspondences with the states matrix (every element in colors array correspond to the respective array in states matrix at same position). So the first element of the colors array will be used as color for every state in the first array of the states matrix (in our case the array has only 1 element: enabled state = hint expanded state for TextInputLayut). Last things states have positive / negative values, and you have only the positive values, so the state "disabled" in android attrs is "-android.state.enabled", the state "not focused" is "-android.state.focused" ecc.. ecc..

    Hope this is helpful. Bye have a nice coding (:

    0 讨论(0)
  • 2020-11-22 15:21

    I tried using android:textColorHint in the android.support.design.widget.TextInputLayout it works fine.

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColorHint="@color/colorAccent">
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Hello"
                    android:imeActionLabel="Hello"
                    android:imeOptions="actionUnspecified"
                    android:maxLines="1"
                    android:singleLine="true"/>
    
            </android.support.design.widget.TextInputLayout>
    
    0 讨论(0)
  • 2020-11-22 15:24

    Can use app:hintTextColor if you use com.google.android.material.textfield.TextInputLayout, try this

     <com.google.android.material.textfield.TextInputLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="@string/app_name" 
         app:hintTextColor="@android:color/white">                   
    
         <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
     </com.google.android.material.textfield.TextInputLayout>
    
    0 讨论(0)
  • 2020-11-22 15:25

    In my case I added this "app:hintTextAppearance="@color/colorPrimaryDark"in my TextInputLayout widget.

    0 讨论(0)
  • 2020-11-22 15:28

    To change color of hint and edit text underline color : colorControlActivated

    To change character counter color : textColorSecondary

    To change error message color : colorControlNormal

    To change password visibility button tint : colorForeground

    For more info on TextInputLayout read http://www.zoftino.com/android-textinputlayout-tutorial

    <style name="MyAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorControlActivated">#e91e63</item>
        <item name="android:colorForeground">#33691e</item>
        <item name="colorControlNormal">#f57f17</item>
        <item name="android:textColorSecondary">#673ab7</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题