Custom Night Resources
// values-night/colors.xml
#511b43
#511b43
#ff6868
Slide 23
Slide 23 text
Default Updated
Slide 24
Slide 24 text
Force Dark Custom DayNight
Background darker
Slide 25
Slide 25 text
Force Dark Custom DayNight
Dividers are dark
Slide 26
Slide 26 text
Force Dark Custom DayNight
Button style
Slide 27
Slide 27 text
Android P
Slide 28
Slide 28 text
Dynamic DayNight mode
AppCompatDelegate.setDefaultNightMode(mode)
getDelegate().setLocalNightMode(mode)
System
Application
Activity
Slide 29
Slide 29 text
Night Modes
● MODE_NIGHT_NO // LIGHT
● MODE_NIGHT_YES // DARK
● MODE_NIGHT_AUTO_BATTERY // DARK IN BATTERY SAVE MODE
● MODE_NIGHT_FOLLOW_SYSTEM // SYSTEM PREFERENCE
Persistence
class App : Application() {
override fun onCreate() {
super.onCreate()
val mode = PreferenceManager
.getDefaultSharedPreferences(this)
.getInt("theme_key", MODE_NIGHT_NO)
AppCompatDelegate.setDefaultNightMode(mode)
Slide 35
Slide 35 text
Fixed
Slide 36
Slide 36 text
Sum up
● Extend DayNight Theme
● Provide additional -night resources for Dark Theme
● Add preference for changing mode inside Application
● Persist preference choice
● Work with designer for the rest
Slide 37
Slide 37 text
Test Dark Theme
Slide 38
Slide 38 text
Test force dark
Slide 39
Slide 39 text
Disable force dark for particular views
view.setForceDarkAllowed(false)
Slide 40
Slide 40 text
Attributes
Slide 41
Slide 41 text
Check is Dark theme applied
fun Context.isDarkTheme(): Boolean {
val mode = this.resources.configuration.uiMode and
Configuration.UI_MODE_NIGHT_MASK
return mode == Configuration.UI_MODE_NIGHT_YES
}
Slide 42
Slide 42 text
AppCompat Material Components
Slide 43
Slide 43 text
AppCompat Material Components
Slide 44
Slide 44 text
Elevation
val bg = MaterialShapeDrawable
.createWithElevationOverlay(this, 32.0f)
// OR
val color = ElevationOverlayProvider(this)
.getSurfaceColorWithOverlayIfNeeded(32.0f)