Search code examples
androidandroid-layoutandroid-relativelayout

RelativeLayout, Unable to resolve id


i was trying to layout views within RelativeLayout like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="TextView" android:id="@+id/upperView"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/bottomView"></TextView>
    <TextView android:text="TextView" android:id="@+id/bottomView"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</RelativeLayout>

and this id is not recognized by android:

 android:layout_below="@id/bottomView"

because i defined the @+id/bottomView" after its call, but sometimes the sort matter to draw who over who, i can solve it from the code, but how fix it within XML


Solution

  • In your layout when define:

    @+id/bottomView"
    

    The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file)

    When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace (Ref.1)

    So, change your xml layout like that:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView 
            android:text="DownTextView" 
            android:id="@+id/upperView"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_below="@+id/bottomView">
        </TextView>
        <TextView 
            android:text="UpTextView" 
            android:id="@id/bottomView"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
        </TextView>
    </RelativeLayout>
    

    Reference:1 Android Layout ID Document