Search code examples
androidglanceglance-appwidget

Specify different colors in night mode for App Widget written with Glance


How I can specify the colors in day vs night mode on a Glance AppWidget and have the system choose the right one when the user turns the night mode on or off?


Solution

  • You have two choices:

    1. You can define a resource that changes in night mode (e.g. defined in res/values-night/colors.xml or res/color-night/mycolor.xml) and use that resource with ColorProvider(R.color.mycolor).
    2. glance-appwidget defines a new function ColorProvider(day: Color, night: Color) which takes both the day and night color, and applies the right one based on the configuration.

    Note

    Prior to Android 12, the color must be fully resolved when the App Widget is created, which means the color will depend on whether night mode is active when the app widget provider generates the App Widget, and there isn't much that can be done about it. But starting Android 12, the color will be resolved by the framework when the App Widget is rendered, so the colors will change instantly when the user turns night mode on or off.