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
Make sure pluginManagement & dependencyResolutionManagement is properly configured in settings.gradle file like this below:
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
}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'
}<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.
android:checkedTo change (on/off) statusapp:backgroundColorOnSwitchOnTo change background color of switch when switch is onapp:backgroundColorOnSwitchOffTo change background color of switch when switch is offapp:thumbColorOnSwitchOnTo change thumb color of switch when switch is onapp:thumbColorOnSwitchOffTo change thumb color of switch when switch is offapp:strokeColorOnSwitchOnTo change stroke border color of switch when switch is onapp:strokeColorOnSwitchOffTo change stroke border of switch when switch is offapp:textColorOnSwitchOnTo change text color of switch when switch is onapp:textColorOnSwitchOffTo change text color of switch when switch is offapp:speedTo change speed of animationapp:strokeWidthTo change size of stroke borderapp:showTextTo turn on or off text visibilityandroid:fontFamilyTo change typeface of status text of switchapp:textOnTo change status text when switch is onapp:textOffTo change status text when switch is off
flexibleSwitch.addOnStatusChangedListener(new FlexibleSwitch.OnStatusChangedListener() {
@Override
public void onStatusChanged(boolean b) {
}
});