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

kelvin gwaruka
kelvin gwaruka
931 Points

I cant get my code to run. Has anyone passed this challenge?

Question: Next, we want to position the ImageView directly below the EditText. We do this using the layout_below attribute. Add this new attribute to the ImageView and set it equal to the ID of the EditText (use "@id/captionField").

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_alignParentLeft="true"
        android:layout_alignParentTop="true"      
        android:hint="Enter a caption" />

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

</RelativeLayout>

2 Answers

andren
andren
28,558 Points

The problem is that you refer to the id using "@+id/" instead of "@id/" the "+" is only used when you are creating a new id, when referencing an existing id you don't include it.

So just drop it from your code like this:

<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:hint="Enter a caption" />

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

And you will be able to pass onto task 3.

kelvin gwaruka
kelvin gwaruka
931 Points

Thanks Andren, but after changing the Layout to

android:layout_below="@id/captionField"/>, I am now getting the error message,

Oops! It looks like Task 1 is no longer passing.

andren
andren
28,558 Points

That's weird, I verified that the code I posted works for both of the tasks and it is based on the code from your answer, the only thing I changed was that I removed the "android:layout_alignParentLeft" since it was redundant after adding the Top alignment.

Try copy and pasting the code I posted and see if that lets you through the two tasks. The code checker for these challenges can sometimes be quite picky and buggy, so it might just be some strange bug that is causing an issue with your code.

In the worse case scenario you can just restart the challenge and do both tasks over again, it shouldn't take too long since you already know the answers now.

kelvin gwaruka
kelvin gwaruka
931 Points

Andren Cheers man. Just copied and pasted your code above and voila managed to complete the challenge. On to the next one.

andren
andren
28,558 Points

Good to hear. Glad I could be of help, and good luck ahead.