151

Can anyone explain the question mark means in Android XML attributes?

<TextView    
    style="?android:attr/windowTitleStyle"
    More attributes
/>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Casebash
  • 114,675
  • 90
  • 247
  • 350

2 Answers2

140

The question mark means it's a reference to a resource value in the currently applied theme. See the linuxtopia Android Dev Guide or the android.com Dev Guide for more about it.

\? escapes the question mark.

ubershmekel
  • 11,864
  • 10
  • 72
  • 89
jball
  • 24,791
  • 9
  • 70
  • 92
  • 3
    I think it would be better to point your second link to: http://developer.android.com/guide/topics/resources/resources-i18n.html#ReferencesToThemeAttributes – Casebash May 03 '10 at 04:39
  • 9
    Dev Guide articles have been restructured, this is the new direct link: http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes – mernen Mar 30 '11 at 06:53
  • 3
    I think it's more appropriate to say ? is used to "Referencing style attributes" rather than "reference to a resource value in the currently applied theme". According to android.com dev guide link you provided. – Helin Wang Apr 14 '14 at 15:58
  • I am still confused, even after reading the two links provided. What does "reference to a resource value in the currently applied theme" mean exactly? Isn't a resource referenced with '@' taken from the current theme too? (for instance, @android:color/black) – Bitcoin Cash - ADA enthusiast Jul 30 '15 at 21:30
  • @Tiago: The at sign (@) is required when you're referring to any resource object from XML. It's not specific to themes. – Julian A. Sep 04 '15 at 19:27
  • 5
    I think it makes the most sense when considering multiple themes containing the same custom resource attribute. Say you have movie-related themes like MyThemeTransformers and MyThemeHobbit, and both have an attribute called movieIcon. And movieIcon points to a @drawable resource, say a robot or a hobbit image, in each theme definition. You can refer to "?attr/movieIcon" anywhere the theme is in effect (like in a toolbar or dialog or whatever kind of View layout), and it will automatically point to the correct drawable when you switch between themes. I hope that helps. – Devon Biere Sep 04 '15 at 19:30
  • 1
    @DevonBiere it helped! It'd be great if you submit your comment as an answer. – Alexander Suraphel Mar 01 '16 at 13:22
47

The ? lets you refer to a style attribute instead of a specific hard-coded resource. See "Referencing style attributes" in the Android Dev Guide for details.

So, how is this actually useful? It makes the most sense when considering multiple themes containing the same custom resource attribute.

Say you have movie-related themes like MyThemeTransformers and MyThemeHobbit, and both have an attribute called movieIcon. And that movieIcon attribute points to a different @drawable resource, say robot.png or hobbit.png, in each theme definition.

You can refer to "?attr/movieIcon" anywhere the theme is in effect (like in a toolbar or dialog or whatever kind of View layout), and it will automatically point to the correct drawable when you switch between themes. You don't need any theme-dependent logic to use the different drawables. You just define the movieIcon attribute for each theme and the Android framework takes care of the rest.

Devon Biere
  • 2,992
  • 1
  • 23
  • 32