1

Kindly help me to create switch widget in android with images that can be change on it's state change.

It should be something like this: enter image description here

Dhruv
  • 1,773
  • 1
  • 14
  • 26

1 Answers1

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
  • 1,971
  • 31
  • 55