gitmyhub

SwitchButton

Java ★ 4.7k updated 2y ago

A cute widget of Switch Button for you to create beautiful and friendly UI.

An Android library that adds a smooth, fully customizable toggle switch widget to your app, with more styling options than the built-in Android switch and an iOS-style rounded appearance.

JavaAndroidGradlesetup: easycomplexity 2/5

SwitchButton is an Android library that provides a customizable toggle switch widget. Android ships with its own switch component, but this library gives developers more control over the appearance and behavior than the built-in version offers. The result looks similar to the on/off toggle found in iOS settings screens: a rounded background that the thumb (the circular knob) slides across.

Usage follows the same pattern as Android's built-in CheckBox. You can call setChecked to turn it on or off programmatically, and toggle to flip its current state. The library adds variants of those calls that skip the sliding animation when you want an instant change, and further variants that change the state without triggering the change listener, which is useful when you need to update the UI from code without causing the listener to fire again.

Styling is controlled through XML attributes or Java code at runtime. You can set separate colors or drawables for the thumb and the background, adjust the margin between them, control corner radius, change animation speed, and add text labels that appear inside the switch to indicate the on and off states. A single kswTintColor attribute lets you apply one color and have all related colors generated automatically. Version 2.0.0 reworked how the widget measures its own size and renamed several attributes to make the meanings clearer, so existing users need to update their attribute names when upgrading.

The library is added to an Android project by adding a single line to the Gradle build file. It is available from Maven Central. A demo app on Google Play shows the full range of styling options. The README is bilingual, with the technical migration notes written in both English and Chinese.

Where it fits