Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color of the toggle button? [duplicate]

Tags:

android

Possible Duplicate:
Change “on” color of a Switch

I need to have a ToggleButton change color when it changes state from Green(true) to Red(false). How can I change ToggleButton color?

like image 679
David Dyon Avatar asked Aug 15 '12 23:08

David Dyon


People also ask

How do I change the toggle button color in bootstrap 5?

navbar-dark: This class is used to set the color of the navigation bar content to light. It will change the toggler icon to have white lines.

How do I change the default button color?

To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.


1 Answers

Create a xml named colors.xml in res/values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#ff0000</color>
    <color name="green">#00ff00</color>
</resources>

In drawable folder, create a xml file my_btn_toggle.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@color/red"  />
    <item android:state_checked="true" android:drawable="@color/green"  />
</selector>

and in xml section define your toggle button:

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New ToggleButton"
    android:id="@+id/toggleButton"
    android:background="@drawable/my_btn_toggle"/>
like image 111
SteveR Avatar answered Sep 22 '22 22:09

SteveR