flexible-switch
It is a responsive switch in android, it can resize itself according to its size. It's recommended to use it with ConstraintLayout to get better result.
Screenrecorder-2022-08-27-15-36-12-387.mp4
USAGE
Just add FlexibleSwitch in your layout XML and flexible-switch library in your project via Gradle:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
...
}
}
dependencies {
implementation 'com.github.codersrouteandroid:flexible-switch:1.0'
}
XML
<com.codersroute.flexiblewidgets.FlexibleSwitch
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="center"
android:textSize="15sp"
app:backgroundColorOnSwitchOn="#827717"
app:backgroundColorOnSwitchOff="#4A148C"
app:thumbColorOnSwitchOff="#FFFFFF"
app:showText="true"
android:checked="false"
app:thumbColorOnSwitchOn="#4A148C"
app:strokeColorOnSwitchOn="#827717"
app:strokeColorOnSwitchOff="#827717"
app:textColorOnSwitchOff="@color/white"
app:textColorOnSwitchOn="#4A148C"
/>
An example activity is in this project to show all the functionalities of this switch, for more information check out this video below:
flexible_switch_example.mp4
Use these following properties in your XML to change your FlexibleSwitch.
Properties:
android:checked
To change (on/off) statusapp:backgroundColorOnSwitchOn
To change background color of switch when switch is onapp:backgroundColorOnSwitchOff
To change background color of switch when switch is offapp:thumbColorOnSwitchOn
To change thumb color of switch when switch is onapp:thumbColorOnSwitchOff
To change thumb color of switch when switch is offapp:strokeColorOnSwitchOn
To change stroke border color of switch when switch is onapp:strokeColorOnSwitchOff
To change stroke border of switch when switch is offapp:textColorOnSwitchOn
To change text color of switch when switch is onapp:textColorOnSwitchOff
To change text color of switch when switch is offapp:speed
To change speed of animationapp:strokeWidth
To change size of stroke borderapp:showText
To turn on or off text visibilityandroid:fontFamily
To change typeface of status text of switchapp:textOn
To change status text when switch is onapp:textOff
To change status text when switch is off
Java
flexibleSwitch.addOnStatusChangedListener(new FlexibleSwitch.OnStatusChangedListener() {
@Override
public void onStatusChanged(boolean b) {
}
});