By default (at least for me) the ToggleButton button has a dark gray color, the power indicator (the bar below) is almost black.

When you press the button, it briefly lights up bright blue, the indicator becomes bright blue.

When the background property changes, the color of the entire button changes, including the indicator. And no backlight when pressed.

Question: how to change the color of the button, without affecting the indicator, and so that the color response to pressing remains?

Well, if possible, change the color of the indicator ..

    2 answers 2

    try this

    Create the colors.xml file in the res/values folder:

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

    In the drawable folder, the file my_btn_toggle.xml is created:

     <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 add ToggleButton in the form:

     android:background="@drawable/my_btn_toggle 

    a source


    There is an option to create your own ToggleButton

    activity_main.xml file

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#d1d4d4" > <!-- ToggleButton по умолчанию --> <ToggleButton android:id="@+id/toggle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- ToggleButton наш --> <ToggleButton android:id="@+id/toggle2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/toggle_selector" android:layout_below="@id/toggle" /> </RelativeLayout> 

    res/drawable/toggle_selector.xml

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

    res/drawable/toggle_state_on.xml

     <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <!-- 5dp border вокруг фигуры --> <stroke android:color="#4c975d" android:width="5dp" /> </shape> </item> <!-- Переписать левую, верхную, правую стороны с фоном нашего цвета --> <item android:bottom="5dp" > <shape android:shape="rectangle"> <solid android:color="#6dd988"/> </shape> </item> </layer-list> 

    res/drawable/toggle_state_off.xml

     <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <!-- 5dp border вокруг фигуры --> <stroke android:color="#b1113e" android:width="5dp" /> </shape> </item> <!-- Переписать левую, верхную, правую стороны с фоном нашего цвета --> <item android:bottom="5dp" > <shape android:shape="rectangle"> <solid android:color="#f51354"/> </shape> </item> </layer-list> 

    the result will be something like this

    enter image description here enter image description here

    a source

    • The result of your example is to change the color of the entire button. At the same time - 1) indicator is lost 2) there is no short-term (flash effect) color change when pressed. - santfos
    • @santfos, well then draw two button icons and assign them instead of colors. - user189127

    enter image description here On the left - the default button, on the right - as needed. You can, of course, draw your images, create your own element ... Is there really no simple way to change the background color without affecting the indicator bar ???