Reputation: 67
Both seem to work in most cases but ?attr/
seems to have compatibility problems. I understand that ?
means we are referring to the current theme value, but what's that doesn't answer my question.
Can someone explain and also mention the best practices?
I am using a theme which inherits from Material theme for v21 and from AppCompat for lower versions. What are the complications I should look out for?
Thanks.
Upvotes: -1
Views: 546
Reputation: 23638
As I blogged about here:
?attr/xyz
Defines and refers to the value of an attribute which you have defined on your own in your application.
?android:attr/xyz -------------------------- Refers to the values of an attribute which are already available in android built-in.
More specifically, the ? implies an extra level of indirection. Think of it as dereferencing an attribute to fetch the resource it points to rather than referring to the attribute itself. You see this with
?android:attr/foo
Which is the best practice to use is depends on what properties you are using and if it is available in built-in android system then you can use it otherwise you can define on your own.
I also answered What's the difference between @android:
and ?android
?
@android: -- This is a reference to existing resource or previously declared style (or drawable or dimension, etc).
?android: -- Is a reference to an item declared in the current Theme*. An example can show this best. I like to think of these as style 'variables', or global attributes that can are set in the main Theme declaration.
From the article which explain about an attributes referencing in android HERE:
Let's say we've got a style that's currently being applied to a stock Widget, the
ExpandableListView
style. For our app, we want to override the groupIndicator and the childDivider styles.<style name="Widget.Holo.ExpandableListView" parent="Widget.Holo.ListView"> <item name="groupIndicator">@android:drawable/expander_group_holo_dark</item> ... <item name="android:childDivider">?android:attr/listDivider</item> </style>
groupIndicator is using an
@android
reference to a stock drawable. To override this, we'll need have create a new style (that has a parent ofWidget.Holo.ExpandableListView
) and override the groupIndicator with a new drawable. We'll then need to set the style on our ExpandableListView, or set the style for ExpandableListViews to use the new style or pass it to the style to the view directly via the constructor.childDivider is using a
?android
attribute reference. This is a lot easier to override. In the main app Theme, add an item that sets this property.
For more details check Referencing attributes
Upvotes: 3