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

gue picture gue · Aug 17, 2011 · Viewed 8.6k times · Source

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>

Answer

gue picture gue · Aug 18, 2011

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>