That line you posted explicitly sets the theme to light mode
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
or to put it another way, "not night mode".
If you want it to toggle, you need to get the current mode (light or dark) and set it to the other one:
// check the mode
val isDarkMode = AppCompatDelegate.getDefaultNightMode() == NIGHT_MODE_YES
// set the other
AppCompatDelegate.setDefaultNightMode(if (isDarkMode) NIGHT_MODE_NO else NIGHT_MODE_YES)
or if you're relying on the switch state, just do if (isChecked)
instead, no need to get
the current mode.
There are more than two modes btw, it's recommended that you handle the "follow the system" one too but that's up to you - more info about the whole thing here from one of the developers.
It also gets into the fact you need to call setDefaultNightMode
whenever your app starts, as early as possible, so it can be themed correctly - e.g. in a custom Application
class, although if you're doing a single-activity app you could stick it in onCreate
there too. Otherwise it would need to go in every Activity
, just in case that's the one that's created first when the app is opened/restored.
But what this means is you have to store the light/dark setting so it can be read when the app starts - it doesn't "stick" to the last thing you set it to. You could use SharedPreferences
for this, and if your switch is an actual Preference
that's all handled automatically! But if it's like a toggle on a normal app screen, you'll have to store it yourself
edit I knew there was something I wanted to mention - you don't need to do activity?.recreate()
, setDefaultNightMode
will handle that for you if it's required (i.e. if there's been a change)