26

Button selectors allows to define a background for each state of the button, but how can the button's text style be defined for each state? My objective is to grey out a button when disabled. So I set a greyed out background to the disable state of the button, but the only way I found to also grey out the text is to change it's color dynamically when I disable the button...

Anybody can help?

Thanks

<style name="srp_button" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/btn_default</item>
    <item name="android:textColor">#ffffff</item> <!-- How to specify different color for different state? -->
    <!-- more stuff -->
</style>

drawable/btn_default.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_enabled="true"><shape>
            <solid android:color="@color/pink" />
            <corners android:radius="6dp" />
        </shape></item>
    <item><shape>
            <solid android:color="#000000" />
            <corners android:radius="6dp" />
        </shape></item>

</selector>
jul
  • 36,404
  • 64
  • 191
  • 318
  • Please put your Code which you have tried... – V.J. Jan 05 '12 at 13:50
  • Just wonder how do you compile @drawable/btn_default without any compilation error. I got No resource found that matches the given name at 'android:background' with value '@drawable/btn_default'. – macio.Jun Jan 02 '15 at 17:53

1 Answers1

44

Well!

You can define the Text color for all 4 state, similarly as you defined background for the Button. For example:

file name: /res/color/text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="#440000"/>
    <item android:state_focused="true" android:color="#004400"/>
    <item android:state_pressed="true" android:color="#000044"/>
    <item android:color="#444"/> 
</selector>

Take this file into your Button Style like this:

<style name="srp_button" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/btn_default</item>
    <item name="android:textColor">@color/text_color</item> <!-- Here is specified text color -->
</style>
Intrications
  • 16,782
  • 9
  • 50
  • 50
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • 1
    Wow, it's cool! The attribute "android:color" in a selector's item is undocumented! But it really works! – Lcsky Aug 11 '12 at 14:48
  • 1
    here is the docu for it http://developer.android.com/guide/topics/resources/color-list-resource.html – AdrianoCelentano Nov 20 '13 at 15:02
  • Thank you very much for mentioning textColor and especially your comment next to it (!-- Here is specified text color -->). I didn't know you can use a normal color selector (with android:color attributes) for textColor as well, and that you can use custom background and custom textColor at the same time. I was googling and reading on Internet for half an hour on how to use text color for the disabled state when you have a custom background (I have a ripple background), I didn't find anything apart from this! Thanks! – Javad Apr 28 '16 at 21:33