Rushikesh Talokar
Rushikesh Talokar

Reputation: 1585

How to change android AppCompatButton's text color on pressed

I have created following style for custom AppCompatButton.

<style name="AppTheme.secondary_button" parent="Base.Widget.AppCompat.Button">
    <item name="colorButtonNormal">@color/secondary_button_color</item>
    <item name="colorControlActivated">@color/menu_blue</item>
    <item name="android:colorButtonNormal">@color/secondary_button_color</item>
    <item name="android:textColorPrimary">@color/gray</item>
    <item name="android:textColor">@color/gray</item>
</style>

What is the attribute to change text color on button pressed?

Upvotes: 3

Views: 1804

Answers (1)

Konstantin Loginov
Konstantin Loginov

Reputation: 16010

To do this, you just need to replace the textColor you specified with a selector:

Something like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/white" />
    <item android:state_focused="true" android:color="@color/grey" /> <!-- (for accessibility) -->
    <item android:color="@color/black" />
</selector>

and then assign it like this: android:textColor="@drawable/button_foreground_selector"

<android.support.v7.widget.AppCompatButton
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    style="@style/CustomAppCompatButtonStyle"
    android:text="Hi, Neo"/>

Not clicked button:

Not clicked

Clicked button:

Clicked

Style I used:

<style name="CustomAppCompatButtonStyle" parent="Base.Widget.AppCompat.Button">
    <item name="colorButtonNormal">@color/red</item>
    <item name="colorControlActivated">@color/blue</item>
    <item name="android:colorButtonNormal">@color/green</item>
    <item name="android:textColorPrimary">@color/gray</item>
    <item name="android:textColor">@drawable/button_foreground_selector</item>
</style>

Upvotes: 3

Related Questions