I've created a basic custom Switch
, as defined below.
<Switch
android:id="@+id/availSwitch"
android:layout_width="wrap_content"
android:switchMinWidth="110dp"
android:layout_height="wrap_content"
android:track="@drawable/switch_track"
android:thumb="@drawable/thumb"/>
The @drawable/thumb
is a simple PNG which works fine.
The @drawable/switch_track
is defined below. @drawable/trackon
and @drawable/trackoff
are PNG's.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/trackoff" />
<item android:state_checked="true" android:drawable="@drawable/trackon" />
<item android:drawable="@drawable/trackoff" />
</selector>
This switch looks and works as intended for the most part, but is there some way to 'animate' the track as the thumb travels over it on user drag? Either fade between checked and unchecked, or preferably change 'behind' the thumb.
The current behaviour is shown below.
For this requirement you need to customize your
Switch
Button FunctionalityYou can visit for demo
Slide Toggle for Android
Android-Switch-Demo-pre-4.0
Custom Toggle Button for Android Applications
You need to understand below two lines .
It has two tag:
android:thumb and android:track
.Thumb will draw the actual look when we swipe or change the status .There was a time I was also looking for the same when I needed a toggle button functionality similar to native iOS which can be dragged to on/off for one of my projects. At that time I searched hard and I found this library.
https://github.com/pellucide/Android-Switch-Demo-pre-4.0
So hope this is what you are looking for too.
You need to make Toggle Button custom
See this