https://kotlinlang.org logo
#compose
Title
# compose
j

Jorge Domínguez

03/08/2024, 2:39 PM
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

Alex Vanyo

03/08/2024, 4:33 PM
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

Jorge Domínguez

03/08/2024, 4:39 PM
I’m using
ComponentActivity
as base, and I’m only using
AppCompatDelegate
if the API is lower than 31
a

Alex Vanyo

03/08/2024, 4:40 PM
For API levels lower than 31, I would expect needing to use
AppCompatActivity
to get the right behavior
j

Jorge Domínguez

03/08/2024, 4:42 PM
I see, but even so I’m noticing the behavior on a Pixel 6a running Android 14
🤔 1
a

Alex Vanyo

03/08/2024, 4:55 PM
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

Jorge Domínguez

03/08/2024, 5:01 PM
Not really, I’m using whatever
isSystemInDarkTheme()
returns to set
MODE_NIGHT_YES
or
MODE_NIGHT_NO
respectively.
j

Jorge Domínguez

03/08/2024, 5:04 PM
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

Alex Vanyo

03/08/2024, 5:16 PM
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

Jorge Domínguez

03/08/2024, 5:28 PM
got it, thanks a lot!
6 Views