Skip to content Skip to sidebar Skip to footer

Change ToggleButton Color

How to change the color of toggleButton in listView? This is how I deigned my toggleButton

Solution 1:

try this way.

ToggleButton :

<ToggleButton 
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:textOn="On"
    android:textOff="Off"
    android:textSize="20sp"
    android:background="@drawable/toggle_day_bg_selector" />

toggle_day_bg_selector.xml

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

toggle_on.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"
    >
    <solid android:color="@color/red" />

</shape>

toggle_off.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <solid android:color="@color/green" />

</shape>

Hope this will help.

EDIT :

use this drawable files for showing images on ToggleButton

toggle_off.xml

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

    <item>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval" >
            <solid android:color="@android:color/holo_green_dark" />
        </shape>
    </item>
    <item android:drawable="@drawable/ic_launcher">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval" >
            <solid android:color="@android:color/holo_green_dark" />
        </shape>
    </item>

</layer-list>

toggle_on.xml

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

    <item>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval" >
            <solid android:color="@android:color/holo_red_dark" />
        </shape>
    </item>
    <item android:drawable="@drawable/ic_launcher">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval" >
            <solid android:color="@android:color/holo_red_dark" />
        </shape>
    </item>

</layer-list>

happy coding..


Post a Comment for "Change ToggleButton Color"