0

I want to change my button's text color when the state of the button changes. For example, when the button is disabled, I want the color the be grey, when it is enabled, I want it to be white and so on. So, to achieve this, I did this in my styles.xml file:

res/drawable/styles.xml

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    ..
    <item name="android:buttonStyle">@style/AppTheme.Button</item>
    ..
</style>

<style name="AppTheme.Button" parent="android:Widget.Button">
    <item name="android:background">@drawable/shadow_button</item>
    <item name="android:textColor">@drawable/shadow_button_text</item>
    ...
</style>

I defined the behavior of the button depending on its states in the shadow_button resource XML file. I decided to make a new one in order to change the text color depending on the state, as suggested in this post:

res/drawable/shadow_button_text.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:color="@color/darkRed" />
    <item android:state_pressed="true" android:color="@color/darkRed" />
    <item android:state_enabled="false" android:color="@color/darkGrey" />
    <item android:color="@color/darkRed" />
</selector>

Unfortunately, nothing changes - the text of the color stays white. What am I doing wrong?

Edit: Sorry, guys, it turned out that I have left textColor="white" on a button in the activity that I'm testing the above code. I removed this tag and everything worked fine.

Community
  • 1
  • 1
Yulian
  • 6,262
  • 10
  • 65
  • 92

1 Answers1

1

May be you are setting button hint not text? because text color only applied on set text not hint, just a check

blackHawk
  • 6,047
  • 13
  • 57
  • 100