How to change the text color of an Android ToogleButton on state change?
Asked Answered
W

2

9

my toggle-Button has different colored backgrounds for each state(red and white). Now I need to change the color of the togglebutton-text(red/white) when activated. With xml I just cannot get it working, perhaps anybody has an idea what I'm doing wrong?

My button in the Layout xml:

<ToggleButton 
android:paddingRight="20dip" 
android:id="@+id/pseudo_tab_right" 
android:layout_weight=".50" 
android:layout_width="wrap_content" 
android:textStyle="bold" 
android:paddingLeft="10dip" 
android:textSize="12sp" 
android:layout_height="wrap_content" 
android:textColor="@drawable/pseudo_tab_text_color"
android:textOff="@string/pseudo_tab_right_text"
android:textOn="@string/pseudo_tab_right_text"
android:background="@drawable/tab_button_right" 
/>  

xml for button-states:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/sort_button_red_right_43" /> 
<item android:drawable="@drawable/sort_button_white_right_43" />
</selector>

And xml for color:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true" android:color="#4f5459" /> 

<!-- focused -->
<item android:state_focused="true" android:color="#4f5459" />

<!-- default -->
<item android:color="#ffffff" /> 

<!-- trying these out, but none works -->
<item android:state_checked="true" android:color="#ff0000" />
<item android:state_enabled="true" android:color="#ff00dd" />
<item android:state_selected="true" android:color="#ff00dd" />
<item android:state_active="true" android:color="#ff00dd" />

</selector>
Wightman answered 17/8, 2011 at 16:54 Comment(0)
W
17

Found it: now I'm using android:state_checked="true" and android:state_checked="false".

color-xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#ffffff" />
    <item android:state_checked="false" android:color="#000000" />
</selector>
Wightman answered 18/8, 2011 at 7:40 Comment(0)
E
-1

You have use something the example below, move the colour attributes to be contained in the item tags.

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

<item android:state_pressed="true" >
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:startColor="#d0785e"
            android:endColor="#000000"
            android:angle="270" />
    </shape>
</item>

<item android:state_focused="true" >
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:endColor="#ffffff"
            android:startColor="#b9b9b9"
            android:angle="270" />
    </shape>
</item>

<item android:state_enabled="false">
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:startColor="#f0aa9f"
            android:endColor="#e21f00"
            android:angle="270" />
    </shape>
</item>

<item>        
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:startColor="#fe9c69"
            android:endColor="#fc5700"
            android:angle="270" />
    </shape>
</item>


</selector>
Endemic answered 17/8, 2011 at 17:7 Comment(1)
Thanks for your help Mike D, I missed that I can get the unchecked/checked Button state by using "android:state_checked="true"" and android:state_checked="false" ;-)Wightman

© 2022 - 2024 — McMap. All rights reserved.