Question mark (?) in XML attributes for Android

AndroidXmlAndroid Styles

Android Problem Overview


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

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

Android Solutions


Solution 1 - Android

The question mark means it's a reference to a resource value in the currently applied theme. See the http://www.linuxtopia.org/online_books/android/devguide/guide/topics/ui/themes.html">linuxtopia Android Dev Guide or the http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes">android.com Dev Guide for more about it.

\? escapes the question mark.

Solution 2 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCasebashView Question on Stackoverflow
Solution 1 - AndroidjballView Answer on Stackoverflow
Solution 2 - AndroidDevon BiereView Answer on Stackoverflow