Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) User Input Using RelativeLayouts and ImageViews

Sean Wilson
Sean Wilson
3,791 Points

Why in this task is it "@id/captionField" and not "@+id/captionField" like we've been taught all along?

All along we've been shown that to get the id of a view, we must use @+id/ as the prefix. But for some reason here we're omitting the '+'. Why is this? What is the rule for omitting it?

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/captionField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:hint="Enter a caption" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/captionField"
        android:layout_alignParentLeft="true"
        android:src="@drawable/grumpy_cat" />

</RelativeLayout>

1 Answer

@id is for reference existing id(s). As you can see, you already declare/define "captionField" on top. @+id is for declaring new id: android:id="@+id/whatever". Now that "whatever" is defined we can use it with android:layout_below, android:layout_toLeftOf, and others. I hope this helps.

Sean Wilson
Sean Wilson
3,791 Points

Cool, this makes sense, thank you. Found this, which explains why we've always seen @+id so far:

The first time that you refer to a particular ID, you have to prefix it with @+id. In actuality, you can prefix every reference to an ID with @+id and everything will work -- in fact, this is what happens if you design your interface with the graphical editor. Just to be safe, everything is prefixed with @+id. The + simply tells it to generate an ID in R.java if and only if it has not already been defined. If you try to define it more than once, it just sees that it's already been defined and continues on normally.

(source: http://stackoverflow.com/questions/12805091/issue-with-id-vs-id-in-positioning-in-relative-layout)