[android] How can I style an Android Switch?

The switch widget introduced in API 14 is styled by default with holo theme. I want to style it slightly different, changing its colors and shape a bit for branding reasons. How does one go about this? I know it must be possible, as ive seen the difference between default ICS and Samsung's touchwiz theme

enter image description here

I assume I'll need some state drawables, and I've seen a few styles in http://developer.android.com/reference/android/R.styleable.html with Switch_thumb and Switch_track that look like what I might be after. I just don't know how to go about using them.

I'm using ActionbarSherlock if that makes a difference. Only devices running API v14 or above will be able to use a switch at all, of course.

The answer is


You can customize material styles by setting different color properties. For example custom application theme

<style name="CustomAppTheme" parent="Theme.AppCompat">
    <item name="android:textColorPrimaryDisableOnly">#00838f</item>
    <item name="colorAccent">#e91e63</item>
</style>

Custom switch theme

<style name="MySwitch" parent="@style/Widget.AppCompat.CompoundButton.Switch">
    <item name="android:textColorPrimaryDisableOnly">#b71c1c</item>
    <item name="android:colorControlActivated">#1b5e20</item>
    <item name="android:colorForeground">#f57f17</item>
    <item name="android:textAppearance">@style/TextAppearance.AppCompat</item>
</style>

You can customize switch track and switch thumb like below image by defining xml drawables. For more information http://www.zoftino.com/android-switch-button-and-custom-switch-examples

custom switch track and thumb


Alternative and much easier way is to use shapes instead of 9-patches. It is already explained here: https://stackoverflow.com/a/24725831/512011


It's an awesome detailed reply by Janusz. But just for the sake of people who are coming to this page for answers, the easier way is at http://android-holo-colors.com/ (dead link) linked from Android Asset Studio

A good description of all the tools are at AndroidOnRocks.com (site offline now)

However, I highly recommend everybody to read the reply from Janusz as it will make understanding clearer. Use the tool to do stuffs real quick


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-theme

How to set Toolbar text and back arrow color Android: making a fullscreen application Android toolbar center title and custom font How do I style appcompat-v7 Toolbar like Theme.AppCompat.Light.DarkActionBar? Change the project theme in Android Studio? Full Screen Theme for AppCompat How to change the background color of Action Bar's Option Menu in Android 4.2? Can't Find Theme.AppCompat.Light for New Android ActionBar Support AlertDialog styling - how to change style (color) of title, message, etc No Title Bar Android Theme

Examples related to styling

CSS change button style after click Horizontal scroll on overflow of table How can I style a PHP echo text? Line break (like <br>) using only css How can I style an Android Switch? How can I style the border and title bar of a window in WPF? textarea's rows, and cols attribute in CSS Use jQuery to hide a DIV when the user clicks outside of it

Examples related to android-styles

How to change the style of a DatePicker in android? Android statusbar icons color How to set Toolbar text and back arrow color Why is my Button text forced to ALL CAPS on Lollipop? How to change color of the back arrow in the new material theme? Changing EditText bottom line color with appcompat v7 Android toolbar center title and custom font How to custom switch button? Style bottom Line in Android AlertDialog styling - how to change style (color) of title, message, etc

Examples related to android-switch

How to custom switch button? How can I style an Android Switch?