Android Developers
Published in

Android Developers

DayNight — Adding a dark theme to your app

Illustration by Virginia Poltrack

How do I use it?

<style name="MyTheme" parent="Theme.AppCompat.DayNight">    <!-- Blah blah --></style>

setDefaultNightMode

  • MODE_NIGHT_NO. Always use the day (light) theme.
  • MODE_NIGHT_YES. Always use the night (dark) theme.
  • MODE_NIGHT_FOLLOW_SYSTEM (default). This setting follows the system’s setting, which on Android Q and above is a system setting (more on this below).
  • MODE_NIGHT_AUTO_BATTERY. Changes to dark when the device has its ‘Battery Saver’ feature enabled, light otherwise.
    New in v1.1.0-alpha03.
  • MODE_NIGHT_AUTO_TIME & MODE_NIGHT_AUTO. Changes between day/night based on the time of day.
    Deprecated in v1.1.0-alpha03.
public class MyApplication extends Application {

public void onCreate() {
super.onCreate();
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
}
}

setLocalNightMode

Activity recreations

How can I check what configuration I’m currently in?

int currentNightMode = getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're in day time
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're at night!
case Configuration.UI_MODE_NIGHT_UNDEFINED:
// We don't know what mode we're in, assume notnight
}

WebViews

System night mode

In-app setting

  • ‘Light’ (MODE_NIGHT_NO)
  • ‘Dark’ (MODE_NIGHT_YES)
  • ‘Set by Battery Saver’ (MODE_NIGHT_AUTO_BATTERY).
    Only show on Android Pie and below. This should be your app’s default when shown.
  • ‘Use system default’ (MODE_NIGHT_FOLLOW_SYSTEM).
    Only show on Android Q and above. This should be your app’s default when shown.

Updating your themes + styles

  • ?android:attr/textColorPrimary. General purpose text color. Will be near-black on light theme, near-white on dark themes. Contains a disabled state.
  • ?attr/colorControlNormal. General-purpose icon color. Contains a disabled state.

Using your own resources for dark/light

<style name="Theme.AppCompat.DayNight" 
parent="Theme.AppCompat.Light" />
<style name="Theme.AppCompat.DayNight" 
parent="Theme.AppCompat" />

Why should I move to using setDefaultNightMode?

  • WebView would reset the Activity configuration as soon as it was loaded into the process. This could lead to later inflated views using the wrong theme.
public class MyActivity extends Activity {

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getDelegate().setLocalNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
}
}

--

--

Articles on modern tools and resources to help you build experiences that people love, faster and easier, across every Android device.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store