154

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

<TextView    
    style="?android:attr/windowTitleStyle"
    More attributes
/>

2 Answers 2

141

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.

7
  • 3
    I think it would be better to point your second link to: developer.android.com/guide/topics/resources/…
    – Casebash
    Commented May 3, 2010 at 4:39
  • 9
    Dev Guide articles have been restructured, this is the new direct link: developer.android.com/guide/topics/resources/…
    – mernen
    Commented Mar 30, 2011 at 6: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
    Commented Apr 14, 2014 at 15:58
  • 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. Commented Sep 4, 2015 at 19:30
  • 1
    @DevonBiere it helped! It'd be great if you submit your comment as an answer. Commented Mar 1, 2016 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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.