Android selector text color
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
On Android, text color changes for states such as pressed, selected, disabled, or focused are usually handled with a color selector, also known as a ColorStateList. Instead of changing text color manually in code for every interaction, you define the state rules once in XML and assign that selector to the view’s textColor.
Use a Color Selector in res/color
For text color, the selector should usually live under res/color, not res/drawable. A typical file might look like this:
Save that as res/color/text_selector.xml.
Android evaluates the items from top to bottom, so the most specific states should come first and the default color should come last.
Apply It to a TextView or Button
Once the selector exists, reference it from your layout:
The same pattern works for Button, AppCompatButton, MaterialButton, and many other text-based widgets, as long as the property you are setting is the text color.
You can also apply it in code:
This is useful when the view is created dynamically or when the selector needs to be applied conditionally.
Know Which States Actually Change
A selector only helps if the view enters the relevant states. For example:
- '
state_pressedapplies while the user presses the view' - '
state_selectedapplies whenisSelectedis true' - '
state_enabledapplies when the view is enabled or disabled' - '
state_focusedis useful for keyboard or TV navigation'
If a state seems not to work, the problem is often not the selector file but the fact that the view never enters that state.
For example, this toggles selected state in code:
If your selector includes state_selected="true", the text color can now change automatically.
Distinguish Text Selectors from Background Selectors
Android uses the same selector idea for many resources, but background selectors and text color selectors are not interchangeable. A drawable selector belongs in res/drawable and controls background resources. A color selector belongs in res/color and produces a ColorStateList.
That distinction matters because many examples online show generic selector XML without clarifying which resource type is being built.
For text color, use a color selector and apply it through android:textColor. Do not point textColor at a drawable selector.
Material and Theme Considerations
In modern apps, theme overlays and Material components can also influence final colors. A selector still works, but you should make sure your chosen colors fit:
- light and dark theme expectations
- accessibility contrast requirements
- component state layers from Material widgets
If you use hard-coded colors everywhere, theme changes become harder later. In many projects it is better to reference color resources inside the selector so the palette stays maintainable.
Common Pitfalls
The biggest mistake is putting the text selector in res/drawable and then wondering why android:textColor does not accept it correctly. Text color selectors belong in res/color.
Another issue is placing the default item before the more specific state items. Since Android reads selectors in order, the default must usually come last.
Developers also sometimes expect state_selected or state_pressed to work automatically when the view never actually enters that state. Check the widget behavior first.
Finally, do not ignore accessibility. A beautiful pressed-state color is still a poor choice if the text becomes unreadable against the background.
Summary
- For Android text color states, use a color selector in
res/color. - Apply it with
android:textColororsetTextColor. - Put specific states first and the default color last.
- Make sure the view actually enters the states you define.
- Keep theme consistency and text contrast in mind when choosing selector colors.

