Jan Bína
02/10/2024, 2:49 PMTextStyle
is of type ColorProvider
and the factory function to create ColorProvider
from resource is restricted to the library and says
Returns a [ColorProvider] that resolves to the color resource. This should not be used outside of the Glance Libraries due to inconsistencies with regards to what process (app vs launcher) colors are resolved inThat's also the case with
colorFilter
for Image
, color of CircularProgressIndicator
and probably more.
On the other hand, there's a .background
modifier that accepts color resource and is working just fine.
I tried to overcome this by using RemoteViews
directly, so for example I can set the color of Text like this:
AndroidRemoteViews(
remoteViews = RemoteViews(context.packageName, R.layout.widget_textview).apply {
setTextViewText(R.id.text, text)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
setTextViewTextColorResource(R.id.text, colorRes)
} else {
setTextColor(R.id.text, LocalContext.current.getColor(colorRes))
}
},
)
and this seems to be working fine too. After all, the glance theme's dynamic colors are also created from resources...
So, what's the reason for that restriction of ColorProvider
? And can my approach with remote views cause troubles in some cases (not resolving to correct color)?bbade_
04/17/2024, 11:04 PMsetTextColor(R.id.text, LocalContext.current.getColor(colorRes))
runs the risk of not updating if the appwidget's host (launcher most likely) switches from light to dark mode. Anything that is using GlanceTheme or resources in the remoteviews will update, but anything that's hardcoded wont.
There's a couple guidelines for using color.
• Create a GlanceTheme
• Don't use resource defined colors, either use dynamic color, or hard code light and dark color values for each of your tokens in the app and give them to a `ColorProvider(day=..., night=...)
You can see some examples of working with color in our demos directory, and in particular, the demos subproject
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:glance/g[…]on-tests/demos/src/main/java/androidx/glance/appwidget/demos/Weronika Nowak
08/05/2024, 2:43 PM