Android - How combining Shape drwable and text color different states for button?
Asked Answered
G

1

6

I have some troubles managing Android State List for a button. I specified some Shape drwable item for different states, but i also need to change textColor depending on the current state.

My actual state list xml is:

<?xml version="1.0" encoding="utf-8"?>    
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true">
        <shape>
            <gradient
                android:startColor="@color/white"
                android:endColor="@color/light_gray"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/classic_red1" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>
        <shape>
            <gradient
                android:startColor="@color/classic_red1"
                android:endColor="@color/classic_red2"
                android:angle="270" />
            <stroke
                android:width="2dp"
                android:color="@color/white" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

I need also to change textColor based on these 2 states. Thanks in advance.

Granulose answered 13/10, 2011 at 15:6 Comment(0)
A
26

selector for the button BG:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape>
        <gradient
            android:startColor="@color/white"
            android:endColor="@color/light_gray"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@color/classic_red1" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>

</item>

<item android:state_focused="true">
    <shape>
        <solid android:color="#424242" />  //another custom shape here for focus state
    </shape>
</item>

<item>
    <shape>
        <gradient
            android:startColor="@color/classic_red1"
            android:endColor="@color/classic_red2"
            android:angle="270" />
        <stroke
            android:width="2dp"
            android:color="@color/white" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>

</item>

selector for the button text color:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/white" /> <!-- pressed -->
    <item android:color="@color/black" /> <!-- default/unchecked -->
</selector>
Anyaanyah answered 13/10, 2011 at 15:12 Comment(2)
Aaargh!! What a fool.. I thought of having to put all in the background drawable resource. Many thanks!Granulose
and the BG selector is a xml under the drawable directory, where the text color one is a xml under the color directory - see #8744084Value

© 2022 - 2024 — McMap. All rights reserved.