Kindly help me to create switch widget in android with images that can be change on it's state change.
Asked
Active
Viewed 3,112 times
1
-
http://stackoverflow.com/questions/23358822/how-to-custom-switch-button – surhidamatya Mar 24 '15 at 11:45
-
In above link, there are labels. I want to display image instead of labels. – Dhruv Mar 24 '15 at 11:46
1 Answers
2
Use as below. It is an edited version of this answer
<Switch
android:id="@+id/th"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textColor="#000000"
app:textOff="@string/switch_no"
app:textOn="@string/switch_yes"
app:thumb="@drawable/apptheme_switch_inner_holo_light"
app:track="@drawable/apptheme_switch_track_holo_light" />
In drawable folder, create a xml file my_btn_toggle.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/red" />
<item android:state_checked="true" android:drawable="@drawable/green"/>
</selector>
where red and green are your desired images.
and in xml section defining your toggle button add:
android:background="@drawable/my_btn_toggle
Hope this helps you.

Community
- 1
- 1

surhidamatya
- 2,419
- 32
- 56