Android
Selector
Text Color
UI Design
App Development

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:

xml
1<?xml version="1.0" encoding="utf-8"?>
2<selector xmlns:android="http://schemas.android.com/apk/res/android">
3    <item android:state_enabled="false" android:color="#FF9E9E9E" />
4    <item android:state_pressed="true" android:color="#FFFF4081" />
5    <item android:state_selected="true" android:color="#FF3F51B5" />
6    <item android:color="#FF212121" />
7</selector>

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:

xml
1<TextView
2    android:id="@+id/statusText"
3    android:layout_width="wrap_content"
4    android:layout_height="wrap_content"
5    android:text="Status"
6    android:textColor="@color/text_selector" />

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:

kotlin
val colors = ContextCompat.getColorStateList(this, R.color.text_selector)
textView.setTextColor(colors)

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_pressed applies while the user presses the view'
  • 'state_selected applies when isSelected is true'
  • 'state_enabled applies when the view is enabled or disabled'
  • 'state_focused is 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:

kotlin
textView.isSelected = true

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:textColor or setTextColor.
  • 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.

Course illustration
Course illustration

All Rights Reserved.