Android customized button; changing text color
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Changing button text color in Android can be as simple as setting one XML attribute, but real apps usually need state-aware colors for pressed, disabled, or selected buttons. The right solution depends on whether the color is fixed, theme-driven, or updated at runtime.
Set a Static Text Color in XML
If the button should always use one color, define it directly in the layout or through a color resource.
And in res/values/colors.xml:
This is the cleanest choice when the button has only one visual state and the color does not depend on interaction.
Use a Color Selector for Different States
Most buttons should not use the same text color in every state. Disabled text should look muted, and pressed text may need higher contrast.
Create a selector in res/color/button_text.xml:
Apply it in the button layout:
The last item acts as the default. Android evaluates the selector from top to bottom and picks the first matching state.
Change the Color Programmatically
If the color depends on app state, change it in code. In modern Android projects, Kotlin is the usual choice.
Using ContextCompat.getColor is safer than deprecated older APIs and keeps the code compatible across API levels.
Theme and Material Considerations
If you use MaterialButton, the visible result may also depend on the background tint, ripple, and theme colors. A text color that looks correct on a plain Button can become unreadable on a filled Material component.
In those cases, define text color together with the button style instead of scattering overrides through many layouts.
Then apply the style where needed:
That keeps the behavior consistent and makes theme changes easier.
Reuse Colors Through Styles Instead of Copying Values
Hard-coded hex values scattered through layouts become difficult to maintain. A better pattern is to keep the actual colors in resources and let layouts reference either a selector or a shared style. That way, a theme adjustment only changes one file instead of dozens of buttons.
For example, a style can combine text color with padding and typography so the button remains consistent across screens:
Then the layout only needs to apply the style. That keeps visual changes predictable and reduces accidental inconsistencies between XML and runtime code.
Common Pitfalls
A common mistake is setting a plain color when the button really needs a ColorStateList. The result looks fine until the button becomes disabled or pressed.
Another issue is overriding text color directly on one screen while the rest of the app uses styles. That makes UI maintenance harder and can produce inconsistent contrast.
The third problem is forgetting that the background matters. A correct text color on a light button may be unreadable after a theme or background tint change.
Summary
- Use
android:textColorfor a single fixed color. - Use a selector resource when button states need different colors.
- Change text color in Kotlin when it depends on runtime logic.
- Prefer styles for reusable Material or AppCompat button designs.
- Always check the final contrast against the actual button background.

