How do I use M3 dynamic colors in my app's launcher icon?

148 views Asked by At

I want to use M3 colors like ?attr/colorPrimary in my app's launcher icon. I know this is possible because my phone's Settings app does it. I've looked around quite a bit and couldn't find anything related to this.

I've tried setting my foreground icon color <path android:fillColor="?attr/colorPrimary" ... />, but this just gave a blank icon.

I even tried setting a tint to the adaptive icon <foreground android:tint="?attr/colorPrimary" ... />, but this didn't have any effect.

1

There are 1 answers

0
Sujit On BEST ANSWER

From the official source code of Android 12's Settings app, it looks like they're using the following private property as the launcher icon's background color.

<color name="icon_launcher_setting_color">@*android:color/accent_device_default_light</color>

This probably works for Android 12 Pixel devices as it's normally not recommended to use private properties because there's a chance they won't exist in the system. In other words, this isn't a good solution.


This is the solution I finally went with which worked nicely for me.

values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="launcher_icon_background_color">@color/m3_sys_color_light_primary</color>
</resources>

values/colors.xml (v31)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="launcher_icon_background_color">@android:color/system_accent1_600</color>
</resources>

And finally I used @color/launcher_icon_background_color in my foreground icon's fillColor.