Simple and Customizable Android Day Night Switch Widget
You can find examples in example project.
- Add Jitpack.io repository in your root build.gradle file
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}
- Add dependency in your module build.gradle file
dependencies {
implementation 'com.github.MasterAlish:Android-DayNightSwitch:v0.1'
}
- Add DayNightSwitch in the xml file
<ma.apps.widgets.daynightswitch.DayNightSwitch
android:id="@+id/switch_1"
android:layout_width="120dp"
android:layout_height="50dp"
app:sliderColor="#F49044"
app:sliderPadding="7dp" />
- Get the reference
val switch1 = findViewById<DayNightSwitch>(R.id.switch_1)
- Listen for updates
switch1.setOnSwitchListener { switch, isDayChecked ->
// Do smth ...
}
- Getting current state
val daySelected: Boolean = switch1.isDayChecked()
- Setting state
switch1.setDayChecked(true, animated = true)
// or
switch1.setDayChecked(true, animated = false)
<ma.apps.widgets.daynightswitch.DayNightSwitch
android:id="@+id/switch_0"
android:layout_width="115dp"
android:layout_height="69dp"
app:dayImage="@drawable/day_3" <!-- Background drawable for Day state -->
app:nightImage="@drawable/night_3" <!-- Background drawable for Night state -->
app:slideDuration="500" <!-- Sliding Animation duration -->
app:sliderColor="#FFFFFF" <!-- Slider color -->
app:sliderPadding="4dp" <!-- Padding for slider -->
app:isDayChecked="false"/> <!-- Initial state -->