I would like to know how to change the image of a switch in android. I know that we can customize the switch and change the color etc. but how do we use another image for the switch. I tried setting the background from my drawables but the switch is still visible and appears on top of my image. I understand that we can use toggle button, or image button however I am curious if we can do so to a switch.
Asked
Active
Viewed 7,997 times
5
-
Possible duplicate of [How to custom switch button?](http://stackoverflow.com/questions/23358822/how-to-custom-switch-button) – Beloo Nov 28 '16 at 09:01
-
Try this [answer](http://stackoverflow.com/questions/28981164/android-switch-change-background-of-switch-at-on-off/28981942#28981942) – dev.bmax Nov 28 '16 at 10:42
2 Answers
9
Override android:thumb and android:track in Switch component to override switch image and background respectiveli. For eg:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/drawable_switch"
android:track="@drawable/drawable_bg" />
where drawable_switch and drawable_bg are selectors respectively.
drawable_switch.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/switch_disabled" />
<item android:state_pressed="true" android:drawable="@drawable/switch_pressed" />
<item android:state_checked="true" android:drawable="@drawable/switch_enabled" />
<item android:drawable="@drawable/switch_default" />
drawable_bg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/switch_bg_disabled" />
<item android:state_focused="true" android:drawable="@drawable/switch_bg_focused" />
<item android:drawable="@drawable/switch_bg_default" />
@Jerry try this and revert if you still have any issues

Hitesh Jain
- 408
- 2
- 5