Skip to content

An beautiful+lightweight+custom-style-easy switch widget for Android minSdkVersion >= 11

License

Notifications You must be signed in to change notification settings

belljay/SwitchButton

 
 

Repository files navigation

Android CI

SwitchButton

Note: this repo is forked and fixed forward on top of the awesome repo of zcweng/SwitchButton.

  • Ugraded API level to 29 so that it's compatiable with latest Google Play store requirements.
  • Codebase is also upgraded to AndroidX for latest reference.
  • Upgraded to support release from jitpack.io for easier installation on android apps.
  • Add Proguards
  • Add support for set button color programatically
  • Some minor discrepency is also fixed.

SwitchButton.An beautiful+lightweight+custom-style-easy switch widget for Android,minSdkVersion >= 11
issues welcome~

Features

-depend without third-part library
-without raw files(pictures/drawables etc...), only one java and style.xml file
-drag switch supported

Integration

gradle:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
...

dependencies {
    implementation 'com.github.belljay:SwitchButton:1.0.4'
}

maven:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
...

<dependency>
    <groupId>com.github.belljay</groupId>
    <artifactId>SwitchButton</artifactId>
    <version>1.0.4</version>
</dependency>

Useage

layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical">

    <com.suke.widget.SwitchButton
        android:id="@+id/switch_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

Activity.java:

com.suke.widget.SwitchButton switchButton = (com.suke.widget.SwitchButton)
    findViewById(R.id.switch_button);

switchButton.setChecked(true);
switchButton.isChecked();
switchButton.toggle();     //switch state
switchButton.toggle(false);//switch without animation
switchButton.setShadowEffect(true);//disable shadow effect
switchButton.setEnabled(false);//disable button
switchButton.setEnableEffect(false);//disable the switch animation
switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        //TODO do your job
    }
});

More Style:

<attr name="sb_shadow_radius" format="reference|dimension"/>       阴影半径  /  Shadow Radius
<attr name="sb_shadow_offset" format="reference|dimension"/>       阴影偏移  /  Shadow Position Offset
<attr name="sb_shadow_color" format="reference|color"/>            阴影颜色  /  Shadow Color
<attr name="sb_uncheck_color" format="reference|color"/>           关闭颜色  /  Unchecked Color
<attr name="sb_checked_color" format="reference|color"/>           开启颜色  /  Checked Color
<attr name="sb_border_width" format="reference|dimension"/>        边框宽度  /  Border Thickness
<attr name="sb_checkline_color" format="reference|color"/>         开启指示器颜色  /  Checked Indicator Line Color
<attr name="sb_checkline_width" format="reference|dimension"/>     开启指示器线宽  /  Checked Indicator Line Thickness
<attr name="sb_uncheckcircle_color" format="reference|color"/>     关闭指示器颜色  /  unchecked Indicator Cicle Color
<attr name="sb_uncheckcircle_width" format="reference|dimension"/> 关闭指示器线宽  /  unchecked Indicator Cicle Thickness
<attr name="sb_uncheckcircle_radius" format="reference|dimension"/>关闭指示器半径  /  unchecked Indicator Cicle Radius
<attr name="sb_checked" format="reference|boolean"/>               是否选中   /  is checked
<attr name="sb_shadow_effect" format="reference|boolean"/>         是否启用阴影  /  Turn on Shadow
<attr name="sb_effect_duration" format="reference|integer"/>       动画时间,默认300ms  /  Animation Duration, default 300ms
<attr name="sb_button_color" format="reference|color"/>            按钮颜色  /  Button Color
<attr name="sb_checkedbutton_color" format="reference|color"/>     开启按钮颜色  /  Checked Button Color
<attr name="sb_uncheckbutton_color" format="reference|color"/>     关闭按钮颜色  /  Unchecked Button Color
<attr name="sb_show_indicator" format="reference|boolean"/>        是否显示指示器,默认true  /  enable indicator, default: true
<attr name="sb_background" format="reference|color"/>              背景色,默认白色  / Background color, default white
<attr name="sb_enable_effect" format="reference|boolean"/>         是否启用特效,默认true / Enable Effect, default true

ScreenShot

Sample Apk:

License

MIT, See the [LICENSE] file for details.

About

An beautiful+lightweight+custom-style-easy switch widget for Android minSdkVersion >= 11

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%