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.
From the HOME Menu select "System Settings" > "System" > "Change Display Colours". Select from one of the following options: Default. Invert Colours.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With