I implemented dark mode for my app. In the settings you can set it. Then it loads your option from sharedprefs and applies it using AppCompatDelegate.setDefaultNightMode. I made a function for this that I call at start of every activity.
fun setAppTheme(context: Context) {
AppCompatDelegate.setDefaultNightMode(when(PreferenceManager.getDefaultSharedPreferences(context).getString("theme", "default")) {
"light" -> AppCompatDelegate.MODE_NIGHT_NO
"dark" -> AppCompatDelegate.MODE_NIGHT_YES
"default" -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
else -> AppCompatDelegate.MODE_NIGHT_YES
})
}
It works great when i set it to follow system, all colors correct, but when I set it to force night mode, and change system mode to light, recycler view items are all light (should be dark!)
I read this, this, tried using all the different contexts, but nothing helped.
I figured it out. The problem was I was calling the function
setAppThemeright beforesuper.onCreate()in main activity. My bad. This works now: