Bart Friederichs
Bart Friederichs

Reputation: 33521

Drawable resources and text color

I am using drawables to nicely style my buttons, and that works fine, except for the text color in the button.

I have defined a state_enabled="false" item in a selector and using setEnabled gives me the right button styles, but I have to jump through quite some loops to get the text color different. This code for example doesn't work (it shows no, or black, text when disabled, and darkgray when enabled):

public void setButtonsEnabled(boolean enable) {
    btnAccept.setEnabled(enable);
    btnDecline.setEnabled(enable);


    int color = R.color.White;
    if (!enable) {
        color = R.color.DarkGray;
    }
    btnAccept.setTextColor(color);
    btnDecline.setTextColor(color);

}

Upvotes: 1

Views: 8878

Answers (3)

Bart Friederichs
Bart Friederichs

Reputation: 33521

I found the solution.

The key lies in also setting the TextColor to a selector in res/colors:

   android:textColor="@color/button_text"
   android:background="@drawable/button_selector" 

For the background selector I used this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/btn_buddy_enabled"></item>
<item  android:state_enabled="false" android:drawable="@drawable/btn_buddy_disabled"></item>
<item  android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/btn_buddy_clicked"></item>
</selector>

And the textColor selector is this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_enabled="true" android:state_pressed="false" android:color="@color/White"></item>
<item  android:state_enabled="false" android:color="@color/Gray"></item>
<item  android:state_enabled="true" android:state_pressed="true" android:color="@color/White"></item>
</selector>

Simply calling setEnabled() will make everything work fine.

Upvotes: 5

Greg Giacovelli
Greg Giacovelli

Reputation: 10184

Have you checked out ColorStateLists? They are pretty awesome. So basically apply all those ideas of Drawable selectors to a set of colors.

Make a folder called [Your Project]/res/colors/ and then put an xml file in there called, button_color.xml (or whatever).

button_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
>
    <!-- Any Enabled button, gets White Text -->
    <item
        android:color="@color/White"
        android:state_enabled="true" />

    <!-- Buttons with any other state, get DarkGray Text -->
    <item
        android:color="@color/DarkGray"/>
</selector>

And then for your TextView, you can just do something like, mTextView.setTextColor(R.color.button_color); At that point there is no need for that if/else type of logic, the selector will do it for you. The selector gets rolled up into the color resource but the class it actually generates is called a ColorStateList in case you find it referenced in other documentation.

Upvotes: 1

Simon
Simon

Reputation: 14472

You are using the wrong value for the color. R.color.White returns the resource ID of the value, not the value itself. Try Color.WHITE, or getResources().getColor(R.color.White)

Upvotes: 1

Related Questions