1

Hi i'm trying to change the color of toggle button's text through xml.

I have referred links but its only changing the background color of toggle button but not its text.

I tried with this approach :

<?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>

but only the background is changing.

Note : I don't want to do it in code since there are 21 toggle buttons and setting listeners for each of them is not good.

yashhy
  • 2,856
  • 5
  • 31
  • 57
  • [http://stackoverflow.com/questions/14065200/android-toggle-text-color-of-togglebutton](http://stackoverflow.com/questions/14065200/android-toggle-text-color-of-togglebutton) – M D Jul 12 '14 at 18:03
  • i'm doing this `` ` ` `` what is wrong in this? – yashhy Jul 12 '14 at 18:57
  • @yasshy Instead of creating `Style` try to set `Drawable` to your `Toggle Button` like `android:textColor="@drawable/text_color_selector"` – M D Jul 12 '14 at 19:03
  • instead of `style="@style/toggleButton"` tried with `android:background="@style/toggleButton"` showing inflation error. – yashhy Jul 12 '14 at 19:14

1 Answers1

6

You shouldn't set the parent of a widget style to be a theme. Instead, you'll want to set it to be the default widget style that you want to modify (e.g. @android:style/Widget.Holo.Button.Toggle).

In your case, however, you don't need to use a style:

res/color/toggle_text.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#ffffff" />
    <item android:color="#000000" />
</selector>

res/layout/your_layout.xml:

...
<ToggleButton
    android:id="@+id/toggleButton"
    ...
    android:textColor="@color/toggle_text" />
alanv
  • 23,966
  • 4
  • 93
  • 80