Supporting Dark Theme · Handle Configuration Changes | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5697228-supporting-dark-theme/lessons/6

Hey Joe,
You said during configuration changes i.e uiMode. We can manually handle the changes to support dark mode. How can we achieve that? Any links for manually changing to dark mode programmatically?

Thanks for the question @sagarsuri!

Just to clarify, are you asking if you turn on uiMode configChanges in an Activity, how do you then programmatically change view themes when in Dark theme? In the video, I show how to use onConfigurationChanged to detect the switch of the system theme. You can then respond to the Dark theme switch in whatever way makes sense for that Activity.

As seen in an earlier video, in terms of your question “Any links for manually changing to dark mode programmatically?”, you use a DayNight theme and call

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

I hope that answers your question, but it’s possible I was not fully understanding the question.

Hey @macsimus
Sorry for not making my question clear. As you have seen that when we change from light to dark mode the Activity is recreated. I wanna avoid that. If you see the Twitter app they don’t recreate the Activity while changing from light to dark mode or vice-versa.

Ah, in that case, I imagine they are just doing typical app theming using custom styles. That is separate from the system Dark theme in Android 10 and also separate from using the AppCompat.DayNight theme. It’s a fair amount more work, but you are able to more directly control the resulting theme.