How to change the background color of the toggle button on Android

I tried to change the background color of the toggle button using the XML file as white, but the toggle button is completely damaged. It looks like the whole button was covered in white.

There is no ON or OFF indication on the toggle button when I changed the color of the toggle button to white. Is there any other way to change the background that will not damage the indication of the toggle button?

<ToggleButton android:id="@+id/togglebutton" android:layout_width="100px" android:layout_height="46px" android:background="#ffffff" android:layout_above ="@+id/save" android:textOn="DAY" android:textOff="NIGHT" /> 

This is how my XML code searches for a toggle button.

+10
android togglebutton
Oct 05 '11 at 6:28
source share
3 answers

Yes, there is a way to change the background as you like, but you need to use a selector like the background:

 <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/some_image" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/some_other_image" /> <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/some_image1" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/other_image" /> </selector> 

For @Drawable etc. (you can use color or make a gradient. this for more information on gradients.

+12
05 Oct 2018-11-11T00:
source share

Follow this method so that your ToogleButton red when turned on and green when OFF

First , create tooglebutton_selector.xml in the drop down folder

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

Second , create togglebutton_on.xml in the excerpt folder

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

Third , create togglebutton_off.xml in the drop down folder

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

Finally in your ToggleButton

  <ToggleButton android:id="@+id/btnMon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/tooglebutton_selector" //set background of ToggleButton to tooglebutton_selector /> 
+1
Aug 14 '16 at 3:54 on
source share

When you decompile SystemUI.apk, you should go to the following file: SystemUI / res / values ​​/colors.xml

Then change the following line:

# ff000000 #ffffffff # 80000000 # ffadc1d6 #ffffffff # ffe6e6e6

-13
Jul 10
source share



All Articles