I thinks it’s a silly question, but I want to defi...
# compose
r
I thinks it’s a silly question, but I want to define a set of colors based on theme like I did with
colors.xml
and
colors-night.xml
(besides the primaryColor, secondaryColor and so on) without using the xml resources.
Copy code
<!-- colors.xml -->
<color name="customers_button_background">#123456</color>
<color name="customers_button_content">#FFFFFF</color>

<!-- colors-night.xml -->
<color name="customers_button_background">#7890AB</color>
<color name="customers_button_content">#000000</color>
How can I achieve this in Compose? Thanks!
y
You can use
isSystemInDarkTheme()
, and return different constants defined anywhere in the code
r
So every time I have to use a custom color I have to check if the system is in dark mode?
y
No, you can provide a different color palette with ambient depending on the state of dark mode. see https://github.com/android/compose-samples/blob/master/Jetsnack/app/src/main/java/com/example/jetsnack/ui/theme/Theme.kt#L82
If the state changes, the ambient values will change, and every affected component using those colors will be recomposed
r
that’s nice, thanks! 😄
y
Note that you should (generally) not directly reference colors in the code, but instead get them from the theme (or your own ambient if you have your own design system implementation). This way you can dynamically update values
r
got it