Search code examples
androidkotlincolors

Which color is colorAccent in Android Studio colors.xml?


Here's my colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="white">#FFFFFFFF</color>
</resources>

I think this was one of the new features in the new Android Studio Version (because I updated Android Studio recently).

Can someone please tell me where colorAccent went?


Solution

  • The colorPrimary, colorSecondary, colorAccent... are attributes defined in the App Theme.

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorOnPrimary">@color/white</item>
        <item name="colorSecondary">@color/white</item>
        <!-- ... -->
    </style>
    

    They have a reference to a color resources defined in colors.xml (like @color/purple_200).

    Using a Theme.MaterialComponents.* your Button is tinted by default by the colorPrimary defined in your theme.

    You can override it using something like:

        <Button
           app:background="?attr/colorSecondary"
    

    or

        <Button
           app:background="@color/...."
    

    If you are using an AppCompat theme the button is tinted by default by the colorAccent defined in your theme.

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <!-- ... -->
        <item name="colorAccent">@color/blue</item>
    </style>
    

    Also in this case you can override it:

        <Button
           app:background="@color/...."