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?
Asked
Active
Viewed 856 times
1 Answers
4
You have two choices:
- You can define a resource that changes in night mode (e.g. defined in
res/values-night/colors.xml
orres/color-night/mycolor.xml
) and use that resource withColorProvider(R.color.mycolor)
. glance-appwidget
defines a new functionColorProvider(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.

PierreBdR
- 42,120
- 10
- 46
- 62