Olson CloudWorks πŸš€

How to change the floating label color of TextInputLayout

September 19, 2026

How to change the floating label color of TextInputLayout

The Android TextInputLayout provides a sleek and user-friendly way to handle EditText hints and labels. One of the common customization requests is: How to change the floating label color of TextInputLayout? The default appearance might not always align with your app’s design, and adjusting the label color can significantly enhance the visual appeal and user experience. This guide will walk you through the various methods to customize the floating label color, ensuring a seamless integration with your application’s theme. We’ll cover XML styling, programmatic approaches, and even dive into handling different states like focused, enabled, and error states. Customizing the floating label color is an effective way to create a polished and professional look for your Android applications.

Understanding TextInputLayout and Floating Labels

TextInputLayout is a crucial component within the Material Components library for Android, designed to wrap EditText and provide enhanced input field experiences. It handles the floating label animation, where the hint text elegantly moves above the input field when the user starts typing. This not only saves screen real estate but also provides a clear visual cue to the user about the purpose of the input field. The floating label is essential for usability and aesthetic appeal, making its customization a high priority for developers aiming for a polished UI. A well-designed floating label can significantly improve the perceived quality of an application.

When customizing the floating label color, it’s important to consider different states of the TextInputLayout. For instance, the color should change when the input field is focused, indicating to the user that it’s active and ready for input. Similarly, when an error occurs, such as invalid input, the floating label color should change to a warning color (typically red) to alert the user. Furthermore, the disabled state should have a subtle color to indicate that the field is not interactive. Managing these states ensures a consistent and intuitive user experience.

According to Google’s Material Design guidelines, using color strategically can draw attention to important elements and guide the user’s eye. The floating label color is one such element that should be used thoughtfully to provide visual feedback and reinforce the app’s branding. By customizing the floating label color, you can create a more engaging and visually appealing user interface. Material Design’s color system recommends using a secondary color for interactive elements to provide visual distinction and enhance usability.

Changing the Floating Label Color Using XML

The most straightforward way to customize the floating label color is through XML styling. This approach allows you to define the color in your app’s theme or directly within the TextInputLayout’s layout definition. By using XML, you maintain a clean separation of concerns, keeping your styling separate from your Java/Kotlin code. This method is particularly useful when you want to apply a consistent style across multiple TextInputLayout instances throughout your application. It promotes reusability and maintainability of your codebase.

To change the floating label color in XML, you can use the app:hintTextColor attribute within the TextInputLayout. This attribute allows you to specify the color for the floating label in its normal (unfocused) state. Additionally, you can use app:hintTextAppearance to apply a custom text appearance, which can include the color and other text-related attributes. Here’s how you can define a custom style in your styles.xml file:

<style name="CustomTextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <item name="hintTextColor">@color/your_custom_color</item> <item name="boxStrokeColor">@color/your_custom_color</item> </style> 

Then, apply this style to your TextInputLayout in your layout XML file:

<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/CustomTextInputLayout"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Hint" /> </com.google.android.material.textfield.TextInputLayout> 

Ensure you define @color/your_custom_color in your colors.xml file. This approach provides a centralized and reusable way to manage your floating label colors. Moreover, using themes ensures a consistent look and feel throughout your app, enhancing the overall user experience.

Programmatically Changing the Floating Label Color

While XML styling is often preferred for static styling, there are situations where you might need to change the floating label color programmatically. This can be useful when the color needs to change dynamically based on user interactions or application state. For example, you might want to change the color based on whether the user has entered valid data or if a particular condition is met within your application logic. This approach offers more flexibility but requires more code.

To change the floating label color programmatically, you can use the setHintTextColor() method on the TextInputLayout instance. This method accepts a ColorStateList, which allows you to define different colors for different states (e.g., focused, enabled, disabled, error). You can create a ColorStateList programmatically or load it from an XML resource. Here’s an example:

TextInputLayout textInputLayout = findViewById(R.id.textInputLayout); int[][] states = new int[][] { new int[] {android.R.attr.state_focused}, // focused new int[] {} // unfocused }; int[] colors = new int[] { Color.GREEN, Color.BLACK }; ColorStateList myColorList = new ColorStateList(states, colors); textInputLayout.setHintTextColor(myColorList); 

This code snippet demonstrates how to create a ColorStateList with different colors for the focused and unfocused states. You can customize the colors array to include the desired colors for each state. Remember to handle different states like error and disabled states to provide a comprehensive and consistent user experience. By programmatically changing the color, you can implement advanced color logic based on the real-time application state.

  • Ensure you handle all possible states of the TextInputLayout (focused, enabled, disabled, error).
  • Use ColorStateList to define different colors for different states.

Handling Error and Focus States

A crucial aspect of customizing the floating label color involves handling the error and focus states. The error state is particularly important because it alerts the user to invalid input, while the focus state provides visual feedback when the input field is active. Properly managing these states enhances the usability and accessibility of your application. Ignoring these states can lead to a confusing and frustrating user experience.

To handle the error state, you can use the setErrorEnabled(true) method on the TextInputLayout to enable error display. Then, use the setError(“Your error message”) method to display the error message. The floating label color will automatically change to the error color defined in your theme (typically red). To customize the error color, you can use the app:errorTextColor attribute in XML or the setErrorTextColor() method programmatically. Similarly, the focus state can be managed by setting a different color in the ColorStateList for the focused state, as demonstrated earlier. This ensures that the user is always aware of the active input field.

Here’s an example of how to handle the error state in XML:

<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:errorEnabled="true" app:errorTextColor="@color/error_color"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Your Hint" /> </com.google.android.material.textfield.TextInputLayout> 

And here’s how to set an error programmatically: textInputLayout.setError(“Invalid input”);. By addressing both error and focus states, you can create a more robust and user-friendly input experience. Proper error handling is essential for preventing user frustration and ensuring data integrity. According to a study by Nielsen Norman Group, clear and concise error messages can significantly improve user satisfaction and task completion rates. Nielsen Norman Group’s error message guidelines emphasize the importance of clear and helpful error messages.

  • Use setErrorEnabled(true) and setError(“Your error message”) to display errors.
  • Customize the error color using app:errorTextColor or setErrorTextColor().

Advanced Customization Techniques

Beyond the basic customization options, there are more advanced techniques to further refine the appearance of your TextInputLayout and floating labels. These techniques involve creating custom themes, using custom drawables, and implementing custom animations. Advanced customization allows you to create a truly unique and branded input experience for your users. However, these techniques require a deeper understanding of Android styling and theming.

One advanced technique is to create a custom theme that extends the Material Components theme and overrides specific attributes related to the TextInputLayout. This allows you to define a consistent style for all TextInputLayout instances within your application. Another technique is to use custom drawables for the floating label background or the input field underline. This can be achieved by creating a custom shape drawable in XML and applying it to the TextInputLayout using the boxBackground attribute. Furthermore, you can implement custom animations for the floating label using ValueAnimator or ObjectAnimator. This allows you to create more engaging and visually appealing transitions.

Here are the steps to create a custom theme:

  1. Create a new style in your styles.xml file that extends the Material Components theme.
  2. Override the textInputStyle attribute to point to your custom TextInputLayout style.
  3. Define your custom TextInputLayout style, overriding the desired attributes (e.g., hintTextColor, boxStrokeColor).
  4. Apply your custom theme to your application in your AndroidManifest.xml file.

By leveraging these advanced customization techniques, you can create a highly polished and branded input experience that sets your application apart. Remember to thoroughly test your customizations on different devices and screen sizes to ensure a consistent and visually appealing user experience. Advanced customization requires careful planning and execution, but the results can be well worth the effort. Android’s documentation on Styles and Themes provides comprehensive guidance on creating custom themes and styles.

Infographic here
FAQ About TextInputLayout Floating Label Color ----------------------------------------------
How do I change the floating label color of TextInputLayout?
You can change the floating label color using XML styling with app:hintTextColor or programmatically with setHintTextColor() method.
How do I handle different states of the floating label color?
Use ColorStateList to define different colors for focused, enabled, disabled, and error states.
How do I customize the error color of TextInputLayout?
Use app:errorTextColor in XML or setErrorTextColor() programmatically to change the error color.
Can I change the floating label color dynamically?
Yes, you can change the floating label color dynamically using setHintTextColor() method in your code.
What is the best way to maintain a consistent style across multiple TextInputLayout instances?
Use XML styling and create a custom theme to ensure a consistent style for all TextInputLayout instances.
We've explored several methods to change the floating label color of TextInputLayout, from simple XML styling to more complex programmatic approaches. By understanding these techniques, you can create a visually appealing and user-friendly input experience for your Android applications. Remember to consider different states like focused, enabled, and error states to provide a consistent and intuitive user interface. Experiment with different color combinations and styling options to find what works best for your application's design. Don't forget to [explore other UI customization options](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c) to further enhance your app's look and feel. Start customizing your TextInputLayouts today and elevate your app's design!

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

Setting colorControlNormal, colorControlActivated, colorControlHighLight in styles does not help.

This is what I have now:

This is what I have now

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>