Android SwitchCompat Example

Photo Credit: Peter Thoeny - Android Switch Button Example

Being an Android developer I always had a development oriented approach. But once you start exploring things from a learning perspective, discoveries are stupendous. I never realized the full potential of AppCompat library in Android, I always thought; it just increases the size of my APK. Jokes apart, in recent turn of events I stumbled upon a situation where I wanted to use an android switch button in my app, where I should be able to drag that switch to an “On” or an “Off” state. To my dismay even this small task created problems. As a standard Android switch which supports the drag animations is only available from SDK v14 and above. I believe many of you must have faced this issue. This inspired me to write this Android switch button example.

Recently in October 14, an update to AppCompat V7 was released, featuring version 21. This version majorly addressed the material design compatibility issues. But a great feature that missed out the spotlight was the Android SwitchCompat widget. This Android switch button not only acts like a standard android switch which can be dragged by holding, but also works fine in Android API 7 and above if AppCompat v7 r21 is included in your project:

In this example I would show how to make an Android switch button work in all Android API versions above 7.

Android SwitchCompat Example

In Android if you want to use a switch widget, which could be dragged into an “on” or “off” position with animation, you can use Android SwitchCompat widget. This widget can work perfectly with any Android 7+ SDK. It is supported through the AppCompat v7 library, which can be included in a project through gradle if you are using Android Studio or through a library project in Eclipse IDE. Lets have look at the Android SwitchCompat Example below:

Android SwitchCompat example screen would look like the screen shot below:

Android SwitchCompat Example

An interesting feature in SwitchCompat button is that you can write text on its drawable thumb button. In the above XML I have shown two buttons, one with app:showText="true" and other without it. To use this property you may need to include xmlns:app="" in the parent layout, as shown above. To make this Android switch button detect state change events you may need implement CompoundButton.OnCheckedChangeListener interface in your main activity:

There is not much to explain in the above code. MainActivity simply implements CompoundButton.OnCheckedChangeListener interface to detect the state changes of Android SwitchCompat button. An interesting fact that turned up while I was developing this was that, somehow SwitchCompat does not acknowledges the padding if it is set through the xml. To set padding in an Android SwitchCompat widget you may need to do it through code, as shown above.

Hope with this Android switch button example, I would see more apps in market where a switch button can be dragged. Connect with us through Facebook, Google+, and Twitter for more Android news.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.

Leave a comment

Your email address will not be published. Required fields are marked *

5 thoughts on “Android SwitchCompat Example