Add Night Mode Support

One of the latest trends with apps and websites is the introduction of dark themes. With Android 10, dark theme support was finally baked into the OS, giving your entire phone a darker look and feel.

In our code, we can either force a mode for the app on the user (though we’re letting them pick which mode they prefer) or we can use the current setting from the system. By having the combination of the Theme.MaterialComponents.DayNight parent style and using mode values from AppCompatDelegate, we can change the entire mode of our app automatically.

The key to making this all work is the AppCompatDelegate.setDefaultNightMode function. This function brings in a mode ID value and instantly switches the app to use the selected ...

Get Kotlin and Android Development featuring Jetpack now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.