0

I'm confused about why we need the + in some sibling references but not others to make views render correctly in the android studio preview pane. (The behaviour when viewing in a running app differs, so is not in scope)

From the android guide docs under the ID section: 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).

From the Relative Layout Params docs android:layout_toLeftOf Positions the right edge of this view to the left of the given anchor view ID. May be a reference to another resource, in the form "@[+][package:]type/name"

From Relative Layout Examples :

In your XML layout, dependencies against other views in the layout can be declared in any order. For example, you can declare that "view1" be positioned below "view2" even if "view2" is the last view declared in the hierarchy. The example below demonstrates such a scenario.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/reminder" />
    <Spinner
        android:id="@+id/dates"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name" <- no + as 'name' already declared?
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/times" /> <- + present as times not declared yet?
    <Spinner
        android:id="@id/times"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentRight="true" />
    <Button
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/times"
        android:layout_alignParentRight="true"
        android:text="@string/done" />
</RelativeLayout>

All this info leads me to believe that the + symbol is only used when referencing siblings, if the sibling is declared after the reference?

However this doesn't appear to be the case in my project:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bg_grey_light"
        android:paddingBottom="10dp">

        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="220dp"
            android:adjustViewBounds="true" />

        <TextView
            android:id="@+id/some_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img" <- This needs a + to render correctly..
            android:text="@{some.name}"/>
    </RelativeLayout>

So what is the rule for the + symbol, and is there any solid documentation that i've missed?

OliverDeLange
  • 643
  • 5
  • 19

1 Answers1

0

From the docs:

The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. 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). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so:

android:id="@android:id/empty"

Defining IDs for view objects is important when creating a RelativeLayout. In a relative layout, sibling views can define their layout relative to another sibling view, which is referenced by the unique ID.

An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).

Hope it will help you.

<- This needs a + to render correctly..

In preview mode, in your case it should render without without a +.

Try adding the tools namespace in the root layout as:

xmlns:tools="http://schemas.android.com/tools"

and then in your TextView:

tools:text="Some name"
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
  • Thanks, i've read the docs already, and i had tools:text included (just took it out for simplicity). I thought the same that it should render without the +, but it doesn't :( – OliverDeLange Apr 22 '18 at 13:33