Thread
#compose
    r

    Răzvan Roșu

    2 years ago
    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.
    <!-- 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!
    Yann Badoual

    Yann Badoual

    2 years ago
    You can use
    isSystemInDarkTheme()
    , and return different constants defined anywhere in the code
    r

    Răzvan Roșu

    2 years ago
    So every time I have to use a custom color I have to check if the system is in dark mode?
    Yann Badoual

    Yann Badoual

    2 years ago
    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

    2 years ago
    that’s nice, thanks! 😄
    Yann Badoual

    Yann Badoual

    2 years ago
    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

    2 years ago
    got it