I temporarily want to disable night mode for my co...
# compose
a
I temporarily want to disable night mode for my compose app. I have used this line to do so for colour scheme
Copy code
val colourScheme = if (darkTheme) lightColourScheme else lightColourScheme
to enforce light colour scheme for now, but for obvious reasons this doesn't apply to
res-night
and I can still see the dark icons and other dark mode artefacts here and there. What is the ideal way to enforce light mode only across the app?
Some solutions suggest inheriting from:
Copy code
Theme.MaterialComponents.Light
And one of the solution is to use:
Copy code
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
s
Perhaps just manually call
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
And
uiModeManager?.setApplicationNightMode(UiModeManager.MODE_NIGHT_YES)
a
@Stylianos Gakis Thank you for the input. One of the comment said that using
Copy code
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
recreates he activity, so I was wondering if it was the ideal way to go .. I guess I will just call this
Also, for some reason the above doesn't seem to work ^^^
I called it in
onCreate
of my launcher activity
s
It does recreate it if you don't opt out of config changes in your manifest, yes.
a
The line is not working for me 😅
s
Could you describe what you mean by "not working" 😅 What did you try, and what did happen instead?
a
I have tried calling the
Copy code
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
In
onCreate()
of Application and in
onCreate()
of my Launcher Activity (Main). But the icons are still from the
res-night
if I dark mode is selected.
i
Keep in mind that AppCompatDelegate only affects AppCompatActivity - if you are extending ComponentActivity or FragmentActivity it won't do anything
🙌 1
a
Ah! Yes, I am indeed extending from
ComponentActivity
I assume that I should now extend from
AppCompatActivity
for my use-case?
i
If you're working with
AndroidView
and want it themed correctly, you should probably be using AppCompatActivity, yes.
👀 1
a
It is an 100% compose app with only the AndroidView used being
NativeAdView
. But yes, I can see nothing going wrong with using
AppCompatActivity
i
🤷‍♂️ you're the one using
res-night
. Only resource worth using in a 100% Compose app are strings
🫢 1
a
Is there an alternate/better way to use image resources for a compose app? At some screens I do need the night version of resources.
Off-topic, but is something this ideal for compose?
Copy code
class LightDrawables: Drawables
class DarkDrawables: Drawables

fun getDrawable(darkTheme: Boolean = isSystemInDarkTheme(), @DrawableRes drawableRes: Int) = if(darkTheme) DarkDrawables.xy else LightDrawables.xy
i
I'd use ImageVector directly rather than go through resources at all if you can. It can help reduce recomposition too: https://engineering.teknasyon.com/reduce-recomposition-for-images-icons-in-jetpack-compose-8d2dd3bfa933
👍 1
💡 1
c
AndroidView used being NativeAdView
Still can't believe there's no first class support for google ads w/ compose
🥲 1