How to actually use a color palette generated by Material Design tool in Android?

1.3k Views Asked by At

I have recently used the material palette generator found at https://material.io/tools/color/ to generate a palette.

This tool is an official tool on Material.io, as you can see. It generated several colors - a primary and secondary, including two variations of each. This led me to create something like this in my colors.xml file.

<!-- Color palette -->
<!-- ... -->

<!-- Colors by usage -->
<color name="colorPrimary">@color/black</color>
<color name="colorPrimaryDark">@color/blackDark</color>
<color name="colorPrimaryLight">@color/gray</color>
<color name="colorSecondary">@color/red</color>
<color name="colorSecondaryDark">@color/maroon</color>
<color name="colorSecondaryLight">@color/redLight</color>
<color name="colorTextOnPrimary">@color/white</color>
<color name="colorTextOnSecondary">@color/white</color>

How can I apply these colors in my theme though? The Android support material (AppCompat) theme only expects/allows three colors - primary, secondary, and accent. Is there another theme I should use where I can apply these attributes? Or am I just doing something wrong here?

I'd like to avoid having to create an entirely new theme here and have to manually set colors for every component that I might want to use.

1

There are 1 best solutions below

0
On

Since I see you're using light and dark colors, I suppose the generator is expecting you to use the DayNight theme. As you're already using AppCompat, I guess you're dealing with the support libraries, so you can easily use those colors with something like this:

     <style name=“AppTheme" parent="Theme.AppCompat.DayNight">
        <item name="colorPrimary">...</item>
        <item name="colorPrimaryDark">...</item>
        <item name="colorAccent">...</item>
        ...
     </style>

It's just a matter of finding the right attributes to set. A quick Google search will bring you many useful resources, check for example this other post that also has a link to the official android developer blog.