Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vasya Drobushkov: Android Dark Theme

Vasya
July 19, 2019

Vasya Drobushkov: Android Dark Theme

How to support dark theme in your Android app

Vasya

July 19, 2019
Tweet

More Decks by Vasya

Other Decks in Programming

Transcript

  1. 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
  2. Persistence class App : Application() { override fun onCreate() {

    super.onCreate() val mode = PreferenceManager .getDefaultSharedPreferences(this) .getInt("theme_key", MODE_NIGHT_NO) AppCompatDelegate.setDefaultNightMode(mode)
  3. 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
  4. 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 }
  5. Elevation val bg = MaterialShapeDrawable .createWithElevationOverlay(this, 32.0f) // OR val

    color = ElevationOverlayProvider(this) .getSurfaceColorWithOverlayIfNeeded(32.0f)