Kobra Kobra - 4 months ago 15
Android Question

Toogle button font color and drawableft selector

Xamarin, android.
I made a selector which changes background colors:



<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape>
<solid
android:color="#aaaaac"/>

<corners
android:radius="6dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
<drawable
drawableLeft="@drawable/male"/>
</item>
<item android:state_checked="true">
<shape>

<solid
android:color="#357fca" />

<corners
android:radius="6dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
<drawable
drawableLeft="@drawable/male_pushed"/>
</item>
</selector>





Besides that, I also want to change drawableft and text colors as follows.
If -
android:state_checked="false"

then

android:textColor="#000000"
and
android:drawableLeft="@drawable/male"


if -
android:state_checked="true"

then
android:textColor="#ffffff"
and

android:drawableLeft="@drawable/male_pushed"


How to do this?
Thank you

sJy sJy
Answer

You can't set multiple properties like drawableLeft, textColor etc using Single selector. You should create selector for each attributes.

Create

  1. drawable_left_selector.xml

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

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false" android:color="#000000"/>
        <item android:state_checked="true" android:color="#ffffff"/>
    </selector>
    
  3. toggle_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false">
            <shape>
                <solid android:color="#aaaaac"/>
                <corners android:radius="6dp" />
                <padding
                    android:left="5dp"
                    android:top="5dp"
                    android:right="5dp"
                    android:bottom="5dp" />
           </shape>
        </item>
        <item android:state_checked="true">
            <shape>
    
                <solid android:color="#357fca" />
                <corners android:radius="6dp" />
                <padding
                    android:left="5dp"
                    android:top="5dp"
                    android:right="5dp"
                    android:bottom="5dp" />
            </shape>
        </item>
    </selector>
    
  4. Set ToggleButton

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/toggle_selector"
        android:drawableLeft="@drawable/drawable_left_selector"
        android:textColor="@drawable/text_color_selector" />