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

Răzvan Roșu

09/25/2020, 2:04 PM
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

Yann Badoual

09/25/2020, 2:08 PM
You can use
isSystemInDarkTheme()
, and return different constants defined anywhere in the code
r

Răzvan Roșu

09/25/2020, 2:14 PM
So every time I have to use a custom color I have to check if the system is in dark mode?
y

Yann Badoual

09/25/2020, 2:16 PM
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

Răzvan Roșu

09/25/2020, 2:19 PM
that’s nice, thanks! 😄
y

Yann Badoual

09/25/2020, 2:21 PM
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

Răzvan Roșu

09/25/2020, 2:52 PM
got it
2 Views