SwitchButton for Android

repository·master·Indexed 43 days ago

https://github.com/panpf/switch-button

A customizable Android UI component providing a switch button with sliding animations, text support, and drawable masking for rounded corners. Extends CompoundButton and allows configuration of frame, state, mask, and slider drawables via XML attributes or programmatic methods. Note: This project is deprecated.

Tokens
739
Snippets
1
Records
3
Agent score
6%

What's inside SwitchButton

  1. Customize SwitchButton image resources

    master

    A SwitchButton is composed of four distinct image layers. You can customize these either via XML attributes or programmatically.

    The Four Components

    1. frameDrawable: The frame image that determines the button size and display area.
    2. stateDrawable: The image representing the ON or OFF state.
    3. stateMaskDrawable: A mask layer used to clip the stateDrawable to a specific shape (e.g., for rounded corners).
    4. sliderDrawable: The image of the sliding handle.

    Method 1: XML Attributes

    Use the app: namespace to define resources directly in your layout file:

    <?xml version="1.0" encoding="utf-8"?>
    <me.panpf.switchbutton.SwitchButton
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="接受推送"
        app:frameDrawable="@drawable/switch_frame"
        app:stateDrawable="@drawable/selector_switch_state"
        app:stateMaskDrawable="@drawable/switch_state_mask"
        app:sliderDrawable="@drawable/selector_switch_slider"/>

    Method 2: Programmatic Setup

    Use setDrawables(Drawable, Drawable, Drawable, Drawable) or setDrawableResIds(int, int, int, int) in your Java/Kotlin code to update the images dynamically.

  2. Add SwitchButton to your layout

    master

    To use SwitchButton in your Android XML layouts, use the me.panpf.switchbutton.SwitchButton tag. It functions similarly to a standard button and can include text.

    Note: This project is deprecated and no longer maintained. It is recommended to use zcweng/SwitchButton instead.

    <?xml version="1.0" encoding="utf-8"?>
    <me.panpf.switchbutton.SwitchButton
        android:id:@+id/switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="接受推送"/>
  3. Configure SwitchButton via code

    master

    Since SwitchButton extends CompoundButton, you can interact with it using standard CheckBox methods (like setting checked state or adding listeners).

    Use the following methods to customize its behavior and appearance:

    MethodDescription
    setDrawables(Drawable, Drawable, Drawable, Drawable)Sets the four component drawables manually.
    setDrawableResIds(int, int, int, int)Sets the four component drawable resource IDs.
    setWithTextInterval(int)Sets the distance between the text and the button (default: 16dp).
    setDuration(int)Sets the animation duration in milliseconds (default: 200).
    setMinChangeDistanceScale(float)Sets the threshold for a successful slide. For example, if the button width is 100 and the scale is 0.2, the user must slide at least 20 units to trigger a state change; otherwise, it rolls back (default: 0.2).