Skip to content

MasterAlish/Android-DayNightSwitch

Repository files navigation

Android-DayNightSwitch

Release

Simple and Customizable Android Day Night Switch Widget

Slider

You can find examples in example project.

Sliders

How to install

  1. Add Jitpack.io repository in your root build.gradle file
allprojects {
	repositories {
		// ...
		maven { url 'https://jitpack.io' }
	}
}
  1. Add dependency in your module build.gradle file
dependencies {
       implementation 'com.github.MasterAlish:Android-DayNightSwitch:v0.1'
}

How to Use

  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" />
  1. Get the reference
val switch1 = findViewById<DayNightSwitch>(R.id.switch_1)
  1. Listen for updates
switch1.setOnSwitchListener { switch, isDayChecked ->
     // Do smth ...
}
  1. Getting current state
val daySelected: Boolean = switch1.isDayChecked()
  1. Setting state
switch1.setDayChecked(true, animated = true)
// or
switch1.setDayChecked(true, animated = false)

Customization: Xml attributes

<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 -->