I’m working on a Compose app that lets users switc...
# compose
j
I’m working on a Compose app that lets users switch between dark, light and system default themes. Currently I’m having a problem where if the user selects the dark theme, the resources I have in the
drawable-night
resource set are not used, and instead the regular
drawable
resources are used, which leads to a very weird-looking UI. I know I can inform the system of the selected theme by doing either
UiModeManager.setApplicationNightMode()
or
AppCompatDelegate.setDefaultNightMode()
, and it seems to work, but for some reason it breaks the theme change when the users selects the system default option. Are there additional considerations for doing this in Compose that I’m missing?
a
Which type of base
Activity
is your Compose app using? If you are using
AppCompatDelegate
, you will need to use
AppCompatActivity
to get all of the backporting in behavior that
AppCompatDelegate
applies.
j
I’m using
ComponentActivity
as base, and I’m only using
AppCompatDelegate
if the API is lower than 31
a
For API levels lower than 31, I would expect needing to use
AppCompatActivity
to get the right behavior
j
I see, but even so I’m noticing the behavior on a Pixel 6a running Android 14
🤔 1
a
Hmm that is surprising. When the user selects the “system default” option, you’re then setting the application night mode back to the AUTO mode?
j
Not really, I’m using whatever
isSystemInDarkTheme()
returns to set
MODE_NIGHT_YES
or
MODE_NIGHT_NO
respectively.
j
I’ll try that, thanks. I guess it’s also worth mentioning that I’m also using
android:configChanges="uiMode"
in my manifest, and noticed that the
onConfigurationChanged
callback is not giving me the right UI_MODE value when I change it in the device’s settings
oh wow, your suggestion seems to work
a
Nice! If you only set
UiModeManager
or
AppCompatDelegate
to “yes” or “no”, you won’t be following the system as that changes, you have to explicity switch back to the setting which follows the system
👌 1
j
got it, thanks a lot!