Android "?colorPrimary" vs "?attr/colorPrimary"?
Asked Answered
A

1

10

I can't find any information about the differences between:

android:textColor="?attr/colorPrimary"

vs

android:textColor="?colorPrimary"

I have read that "?attr" means attribute value specified in the current theme, but without "attr" it gives the same result (=color defined in my theme). It behaves similar with other attributes?

For example:

Does android:background="?attr/selectableItemBackground" equals android:background="?selectableItemBackground" ?

Here it's told that it differs.

Thanks very much.

Almanac answered 6/6, 2017 at 13:45 Comment(5)
Isnt this question a repeat of the one you linked?Mckenney
Reading it briefly, it looks like attr/selectableItemBackground will change depending on the theme, where as the other will not. its likely equal in your scenario as the default is the theme you are using. Try changing theme then comparing.Mckenney
Changing the theme does not change the color for any case. In both cases it produces color defined by me (colorPrimary). I use theme that inherits Theme.AppCompat.Light.DarkActionBar.Almanac
If you are overriding the primary colour, I wouldn't expect it to change at all. The other answer on that identical question suggests they are all the same. Just different ways of writing it. One possibly being shorthand to the other. Either way, i wouldn't worry about it unless you are having some kind of issue?Mckenney
Ok, I found the answer in the docs. See my own post. Thanks.Almanac
A
6

Ok, I have finally found in the docs:

Because the system resource tool knows that an attribute resource is expected in this context, you do not need to explicitly state the type (which would be ?android:attr/textColorSecondary)—you can exclude the attr type.

So, I deducted it's like this: the AppCompat library attributes are treated as custom attributes and as a result we point them by a question mark without "android" keyword. And "attr" is type of resource which is optional for both platform and custom attributes (from docs). Android documentation

Almanac answered 6/6, 2017 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.