Night mode color values not applying to recycler view items

192 Views Asked by At

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.

1

There are 1 best solutions below

0
kuky.isnt.bot On

I figured it out. The problem was I was calling the function setAppTheme right before super.onCreate() in main activity. My bad. This works now:

...
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setAppTheme(this)
...