Why backgroundTint requires android: prefix after API 21 but not before?
Asked Answered
U

2

7

Declaring a style, when I define an item android:backgroundTint, I get a warning that this is available as of API 21 onward, while my minimum API specified is lower (API 17). On the other hand, when I replace that with simply backgroundTint, the warning is gone. Does anyone know why is that?

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:backgroundTint">#0F0</item>
</style>

Aside, from that, if I use android:backgroundTint for a single component, for example a button, I get no warning or error, no matter what my project's minimum SDK is. This is somewhat puzzling.

Unbacked answered 16/2, 2018 at 11:4 Comment(1)
Cause backgroundTint introduced in API21 . Read Here. So this Style belongs to style v-21 . And for warning i think this is happening because backgroundTint individually not an attribute so not getting compiled .Astrograph
B
4

Why it works at runtime: AppCompat support library backports many of the later API level functionality and its styles define the prefix-free backgroundTint attribute.

Additional reason why lint does not complain: style attributes not prefixed with android: are not validated for known attribute names. You can actually put any string in item name attribute. For example:

<item name="iJustInventedThis">foo</item>

In layout widgets you get lint complaining about missing prefixes or unknown attributes. If you have an AppCompat widget such as AppCompatImageView, then you can use the backgroundTint attribute.

Bereft answered 16/2, 2018 at 11:6 Comment(3)
So, you can use any value for name in styles, but not for individual elements like Button, EditText, etc. As they need prefixes. Right?Unbacked
In layout widgets you get lint complaining about missing prefixes or unknown attributes.Bereft
Might seem late, but your first paragraph raises my question. "... and its styles define the prefix-free backgroundTint attribute". Is that what you meant? I have a little problem understanding it.Unbacked
M
6

I know it's an old question, but if you follow this solution that instructs us to change android:backgroundTint to app:backgroundTint, you get rid of the following warning:

Attribute backgroundTint is only used in API level 21 and higher

Misusage answered 18/12, 2018 at 3:49 Comment(2)
Thanks for that! It indeed works with AppCompat activities or AppCompat views :)Margheritamargi
Great to know it helped you, @xarlymg89! As it helped a lot while I was a looking for a similar question, I thought it could help others and it's good to know it was really useful for another developer.Misusage
B
4

Why it works at runtime: AppCompat support library backports many of the later API level functionality and its styles define the prefix-free backgroundTint attribute.

Additional reason why lint does not complain: style attributes not prefixed with android: are not validated for known attribute names. You can actually put any string in item name attribute. For example:

<item name="iJustInventedThis">foo</item>

In layout widgets you get lint complaining about missing prefixes or unknown attributes. If you have an AppCompat widget such as AppCompatImageView, then you can use the backgroundTint attribute.

Bereft answered 16/2, 2018 at 11:6 Comment(3)
So, you can use any value for name in styles, but not for individual elements like Button, EditText, etc. As they need prefixes. Right?Unbacked
In layout widgets you get lint complaining about missing prefixes or unknown attributes.Bereft
Might seem late, but your first paragraph raises my question. "... and its styles define the prefix-free backgroundTint attribute". Is that what you meant? I have a little problem understanding it.Unbacked

© 2022 - 2024 — McMap. All rights reserved.