On recent Android versions, ever since Android 8.1, the OS got more and more support for themes. More specifically dark theme.
Even though there is a lot of talk about dark mode in the point-of-view for users, there is almost nothing that's written for developers.
Starting from Android 8.1, Google provided some sort of dark theme . If the user chooses to have a dark wallpaper, some UI components of the OS would turn black (article here).
In addition, if you developed a live wallpaper app, you could tell the OS which colors it has (3 types of colors), which affected the OS colors too (at least on Vanilla based ROMs and on Google devices). That's why I even made an app that lets you have any wallpaper while still be able to choose the colors (here). This is done by calling notifyColorsChanged and then provide them using onComputeColors
Starting from Android 9.0, it is now possible to choose which theme to have: light, dark or automatic (based on wallpaper) :
And now on the near Android Q , it seems it went further, yet it's still unclear to what extent. Somehow a launcher called "Smart Launcher" has ridden on it, offering to use the theme right on itself (article here). So, if you enable dark mode (manually, as written here) , you get the app's settings screen as such:
The only thing I've found so far is the above articles, and that I'm following this kind of topic.
I also know how to request the OS to change color using the live wallpaper, but this seems to be changing on Android Q, at least according to what I've seen when trying it (I think it's more based on time-of-day, but not sure).
Is there an API to get which colors the OS is set to use ?
Is there any kind of API to get the theme of the OS ? From which version?
Is the new API somehow related to night mode too? How do those work together?
Is there a nice API for apps to handle the chosen theme? Meaning that if the OS is in certain theme, so will the current app?
Use the system setting (Settings -> Display -> Theme) to enable Dark theme. Use the Quick Settings tile to switch themes from the notification tray (once enabled). On Pixel devices, selecting the Battery Saver mode enables Dark theme at the same time.
Positive contrast polarity (light mode) refers to dark-font text on light background. Negative contrast polarity (dark mode) denotes the combination of light (e.g., white) text on dark (e.g., black) background.
Google unveiled its long-awaited system-wide dark theme back on Android 10. Most Google apps adapt automatically to dark mode once you set it up, but others will need to be toggled manually.
Google has just published the documentation on the dark theme at the end of I/O 2019, here.
In order to manage the dark theme, you must first use the latest version of the Material Components library: "com.google.android.material:material:1.1.0-alpha06"
.
Change the application theme according to the system theme
For the application to switch to the dark theme depending on the system, only one theme is required. To do this, the theme must have Theme.MaterialComponents.DayNight as a parent.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight"> ... </style>
Determine the current system theme
To know if the system is currently in dark theme or not, you can implement the following code:
switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) { case Configuration.UI_MODE_NIGHT_YES: … break; case Configuration.UI_MODE_NIGHT_NO: … break; }
Be notified of a change in the theme
I don't think it's possible to implement a callback to be notified whenever the theme changes, but that's not a problem. Indeed, when the system changes theme, the activity is automatically recreated. Placing the previous code at the beginning of the activity is then sufficient.
From which version of the Android SDK does it work?
I couldn't get this to work on Android Pie with version 28 of the Android SDK. So I assume that this only works from the next version of the SDK, which will be launched with Q, version 29.
Result
A simpler Kotlin approach to Charles Annic's answer:
fun Context.isDarkThemeOn(): Boolean { return resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With