3

I implemented the dark theme using DayNight material themes in my app. I followed several articles and conference talks on the internet. Everything worked well until some small things started to occur. Let me explain:

The app has several activities. In order to not theme every activity explicitly, I followed advice to put the initial theme setting in my Application's onCreate() method. This has one drawback though, which I will explain next.

1.) AppCompat implements night mode at the activity level, which means that it won't update the application context (which I am using to set the theme app wide) (source: https://issuetracker.google.com/issues/134379747)

2.) The following piece of code is recommended to check whether or not the app is running in which mode. But it returns exactly the opposite mode in my case:

val currentNightMode = configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
when (currentNightMode) {
    Configuration.UI_MODE_NIGHT_NO -> {} // Night mode is not active, we're using the light theme
    Configuration.UI_MODE_NIGHT_YES -> {} // Night mode is active, we're using dark theme
}

3.) When setting my app to follow system and then I manually switch to light mode (in the app) and then come back to follow system, my app stays light even tho my phone is in system-wide dark theme. It does change however when toggling my app's theme.

What am I doing wrong? Could a possible solution be to set the theme on an activity level?

Andre Thiele
  • 3,202
  • 3
  • 20
  • 43

2 Answers2

2

in your resources folder you can add a bools.xml in your value_night folder with below code

<resources>
    <bool name="is_night_mode">true</bool>
</resources>

enter image description here

and in default folder make false

  <resources>
        <bool name="is_night_mode">false</bool>
    </resources>

enter image description here

and in class file access it like Boolean isNightTheme = context.getResources().getBoolean(R.bool.preferences_autoplay);

Hope it helps.

Community
  • 1
  • 1
1
            when (resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) {
                Configuration.UI_MODE_NIGHT_NO -> themeLight.isChecked = true
                Configuration.UI_MODE_NIGHT_YES -> themeDark.isChecked = true
                Configuration.UI_MODE_NIGHT_UNDEFINED -> themeLight.isChecked = true
            }

use the above code to get the current theme. Inorder to change the theme instantly you need to add

 AppCompatDelegate.setDefaultNightMode(themeMode)
Abraham Mathew
  • 2,029
  • 3
  • 21
  • 42
  • There are som threads on SO where people state the same problem. My theme does not change from light to dark when Follow System is selected even tho my phone is in dark mode – Andre Thiele May 25 '20 at 11:57
  • Inorder for the change to take action instantly you need to restart your activity please have a look at the above code.I have added some more details – Abraham Mathew May 25 '20 at 12:33
  • It works with auto battery for example. Really weird. I change my apps theme to follow system and it does not recognize the system theme but trying this with battery saver and it works. I think this is some problem of the API – Andre Thiele May 25 '20 at 13:04
  • actually,can you please add the code where you change the theme – Abraham Mathew May 25 '20 at 13:17