Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android change switch image

Tags:

java

android

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.

like image 259
JerryKo Avatar asked Nov 28 '16 08:11

JerryKo


People also ask

How do I change the color of my Switch?

From the HOME Menu select "System Settings" > "System" > "Change Display Colours". Select from one of the following options: Default. Invert Colours.


1 Answers

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

like image 77
Hitesh Jain Avatar answered Sep 27 '22 17:09

Hitesh Jain