Issue
I have an app widget with a white background. Some of the text that displays uses ?android:textColorPrimary, some uses ?android:textColorSecondary, and some use colours I've defined.
For some reason though, when I run my app on pre Nougat (24 or lower), the colours are white such that the text is invisible on the white background, but anything 24 and higher shows as black or grey. The colours I've defined are always ok.
What's also interesting is that the code in the app widget is almost identical to the actual app (both displaying a list of items) and the app version (even on these older API's) uses dark colours but the widget for some reason selects white colours for the text.
If I trace through the XML code in the styles I get to this:
<!-- The most prominent text color. -->
<attr name="textColorPrimary" format="reference|color" />
What does this mean? How does it know what the most prominent text colour is? Why is it different in the widget vs the app, and why only on older API versions?
Is there anything I can do to fix this so that it's consistent? I'm using the same theme. Why would this happen?
Note: as far as just setting a specific colour, this is not what I'm asking. Sure I could just set the text to black or something but I want to use these styles so that in the future the colour can change as necessary, especially since I'm using the day/night theme. Maybe it's related to that? Ok I tried a normal theme but the problem persists (text is invisible on the widget - only the widget - on older than API 24).
Please let me know if anything is unclear and I'll update the question. Thanks.
Solution
What does this (textColorPrimary) mean?
This means, that the value specified in the current theme's android:textColorPrimary would be applied. So, if you have declared a TextView in xml and have applied android:textColor="?android:textColorPrimary" to it, then this attribute would be fetched from the theme of the current context with which this layout is being inflated.
How does it know what the most prominent text colour is?
It is fetching that value from the theme that you have applied to your activity or from the context with which the view is being inflated (see ContextThemeWrapper and android:theme). It may differ from platform version to platform version. Depending on the theme you are using, it may differ, see themes.xml.
You can override that attribute in your theme:
<style name="AppTheme" parent="...">
...
<item name="android:textColorPrimary">@color/someColor</item>
</style>
Now, you have successfully overridden android:textColorPrimary attribute, so hereafter any view that is being inflated with a context of this theme would see this overridden value when referring to ?android:textColorPrimary.
Why is it different in the widget vs the app, and why only on older API versions?
Your widget may have been inflated with a particular theme, while app has a different theme. Had they the same theme - those attributes would be same.
Answered By - azizbekian
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.