DennisVA
DennisVA

Reputation: 2119

Layout id has to be added again after its specified

I have this very simple layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dp">

<TextView
    android:id="@+id/textViewBottom"
    style="@style/NLBTextAppearanceSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Information"/>

<TextView
    android:id="@+id/textViewTop"
    style="@style/NLBTextAppearance"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur condimentum tortor quis quam hendrerit, a rhoncus mauris porta. In at nisl et arcu consequat placerat. Integer in ipsum lectus. Proin elementum faucibus odio, in sodales dui tristique eget"
    android:layout_above="@+id/textViewBottom" />

</RelativeLayout>

I don't understand why i have to do @+id/textViewBottom again in android:layout_above of textViewTop; after the id has been added already in textViewBottom. If i remove the "+" sign from that line, the views overlap eachother. I never had issues with this before ...

Upvotes: 0

Views: 52

Answers (3)

Eselfar
Eselfar

Reputation: 3869

When the app is built, it creates a file to reference all id added in your layouts. Then you can use these id to retrieve views or to place a view based on another one.

The +id/ add the id to this list of references, when id/ only use it.

You can't use a reference before it has been declared, so the first time should always be +id/ even if it not on the view defined by the id.

If I use your example:

<!-- VIEW 1 -->
<TextView
    android:id="@+id/textViewBottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#fff9e5"
    android:text="Information" />

<!-- VIEW 2 -->
<TextView
    android:id="@+id/textViewTop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/textViewBottom"
    android:background="#e5ffe5"
    android:text="Lorem ipsum dolor sit amet" />

The VIEW 1 declares the id textViewBottom so, when you want to position the VIEW 2 above it, you just need to use id as textViewBottom has already be declared.

If you change the order of the views like that:

<!-- VIEW 2 -->
<TextView
    android:id="@+id/textViewTop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/textViewBottom"
    android:background="#e5ffe5"
    android:text="Lorem ipsum dolor sit amet" />


<!-- VIEW 1 -->
<TextView
    android:id="@id/textViewBottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#fff9e5"
    android:text="Information" />

Now VIEW 2 needs to declare textViewBottom in order to use it as it has never been declared before. Otherwise the view is not gonna be placed correctly. So now, when you want to add the id on the VIEW 1 you don't need to do +id/ as the id has already been declared.

Upvotes: 0

ND1010_
ND1010_

Reputation: 3841

If you use id for a reference purpose in RelativeLayout then you only use @id/ instead of @+id/ if that id already given to any VIew

That means your R.java file have already registered that Id that's why only need to use @id/

Upvotes: 1

Mashuk Khan
Mashuk Khan

Reputation: 67

You are giving android:layout_above="@+id/textViewBottom to make that Textview above the TextView named textViewBottom.

It's property of RelativeLayout Just go thorough Relativelayout and its basic properties to understand it clearly.

Upvotes: 1

Related Questions