44

What's the difference between "?android:" and "@android:" in an android layout xml file? They seem to be interchangeable ways of reusing android SDK resources.

The only difference I discovered is illustrated by the following example.

Here the TextView's right edge is rendered aligned with the left edge of the ImageButton

 <RelativeLayout
    android:id="@id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#888888">
    <TextView
        android:id="@android:id/text1"
        android:layout_alignParentLeft="true"
        android:text="blah blah"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@android:id/button1" />
    <ImageButton
        android:id="@android:id/button1"
        android:layout_alignParentRight="true"
        style="@style/PlusButton" />
</RelativeLayout>

Here, however, the right edge of the TextView is aligned with the right edge of the RelativeLayout. The TextView overlaps the ImageButton.

<RelativeLayout
    android:id="@id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#888888">
    <TextView
        android:id="@android:id/text1"
        android:layout_alignParentLeft="true"
        android:text="blah blah"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="?android:id/button1" />
    <ImageButton
        android:id="?android:id/button1"
        android:layout_alignParentRight="true"
        style="@style/PlusButton" />
</RelativeLayout>

The only difference between the two layouts is the use of @android vs ?android. Both compile with no errors.

Thanks much.

Julian A.
  • 10,928
  • 16
  • 67
  • 107
  • Possible duplicate of [Question mark (?) in XML attributes for Android](http://stackoverflow.com/questions/2733907/question-mark-in-xml-attributes-for-android) – Samuel Katz Feb 20 '16 at 04:06

1 Answers1

36

Prefixing the ID with a question mark indicates that you want to access a style attribute that's defined in a style theme, rather than hard-coding the attribute.

See "Referencing Style Attributes" here: accessing-resources

UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
Jason LeBrun
  • 13,037
  • 3
  • 46
  • 42
  • 4
    More specifically, the ? implies an extra level of indirection. Think of it as dereferencing a theme attribute to fetch the resource it points to rather than referring to the attribute itself. You see this with `?android:attr/foo`. – adamp Jan 23 '11 at 03:22
  • 10
    I wish there was more explanation in this answer – CodyBugstein Aug 27 '14 at 21:44
  • @Imray, here is an example: I found these styles from the themes.xml file found in sdk\platforms\android-L\data\res\values. ` ` The items are referenced in on the attrs.xml file on the same directory. – chjarder Aug 28 '14 at 03:00
  • They are using the same name which means they represent the same resource. So when you switch between these 3 themes, the attr you are using will get the item specified in the chosen theme and use it as the drawable. Sorry if my answer is talking about drawables, but this is the best example I can provide. – chjarder Aug 28 '14 at 03:09