10

I use RelativeLayouts extensively in my app and thought I knew how to specify them, but this has me at a loss. I am basically positioning 4 TextViews in two rows of two each consisting of a label and text that will be supplied. It should look something like:

Born: 23 Aug 1810 Mason Co., Kentucky

Died: 15 Jul 1865 Cincinnati, Hamilton Co., Ohio

This is the relevant portion of the layout:

        <TextView android:id="@+id/birth_lbl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/given_layout"
            android:layout_alignLeft="@+id/given_layout"
            android:layout_marginTop="6dip"
            style="@style/label"
            android:text="@string/born"
        />
        <TextView android:id="@+id/birth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/birth_lbl"
            android:layout_alignBaseline="@+id/birth_lbl"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="6dip"
            style="@style/main_text"
            android:text="dd Mmm yy"
        />
        <TextView android:id="@+id/death_lbl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/birth"
            android:layout_alignLeft="@+id/birth_lbl"
            android:layout_marginTop="4dip"
            style="@style/label"
            android:text="@string/died"
        />
        <TextView android:id="@+id/death"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/death_lbl"
            android:layout_alignLeft="@+id/birth"
            android:layout_alignBaseline="@+id/death_lbl"
            android:layout_marginRight="6dip"
            style="@style/main_text"
            android:text="dd Mmm yy"
        />

For some reason, this displays the death line views ABOVE the birth line views! If I change the spec of the death_lbl view to instead be 'layout_below="@+id/birth_lbl"', the lines are positioned correctly! However, it is possible for the "birth" view to wrap to multiple lines, so I really need to position the 2nd line below "birth", not "birth_lbl".

Anyone know the reason for this behavior? It occurs both in the Graphical Layout editor in Eclipse and at runtime on my tablet running Android 4.0.

gordonwd
  • 4,537
  • 9
  • 37
  • 53
  • 1
    The `+` sign in the `id` declaration should be used only on the first appearance of the `id`. You should edit and post the full layout file to see the extra views that you use (`given_layout`?) . I tried it in the emulator and for my it is working, but of course i don't have the extra `given_layout` view. – user Feb 19 '12 at 19:07
  • Its because, as Slukian said, you don't even know what you are doing... Stop using the '+' wherever you want and try considering that each + you have is like creating a new variable... It's even pretty strange that the compiler doesn't say a word on that... – Cehm Feb 19 '12 at 19:12
  • 4
    More gently here! The android 21 compiler puts these plusses in when you use the "design". The reason we ask questions is that, as you say, we don't know. – user462990 Jan 05 '15 at 17:10
  • 1
    In my case I need to add `+` to make it work, e.g. `android:layout_below="@+id/someId"`, it's weird since it already defined with `+` on top. – 林果皞 Nov 01 '17 at 10:00

5 Answers5

17

Try changing android:layout_below="@+id/birth" in death_lbl to android:layout_below="@id/birth", because at this point it is already declared, which the + implies, it could lead to problems when declaring it again.

Demonick
  • 2,116
  • 3
  • 30
  • 40
  • Thanks. I actually knew that and what you saw in my sample code was something I tried in desperation :-). I put it back the way it was with the same result. – gordonwd Feb 19 '12 at 23:51
  • but when i do it,it's not work and when put + , it's work!why? – Dr.jacky Oct 29 '12 at 06:15
7

I was also facing the same problem because I was using constraint layout, but align_below works only in Relative Layout. so check which layout you are using.

Deepak Gautam
  • 185
  • 4
  • 11
1

If fixing your + signs doesn't help, you could always position your id/death below id/birth, and then put id/death_label toLeftOf id/death.

aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
dokkaebi
  • 9,004
  • 3
  • 42
  • 60
  • Tried that and it just doesn't like putting anything under that id/birth view. I'm going to try coding up a more isolated case in a separate file before going further with this. Thanks! – gordonwd Feb 19 '12 at 23:52
1

I was actually able to duplicate this phenomenon by coding up a temporary layout with only those fields, and was able to determine that the problem went away if I did not position the initial birth_lbl view relative to the view above it ("given_layout" in this case).

So I'm not sure if this is classified as a fix, a workaround, or a kludge (is that still a word these days?), but what I did was to position the text views inside their own RelativeLayout and position the RelativeLayout relative to id/given_layout. In any case, it works...

aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
gordonwd
  • 4,537
  • 9
  • 37
  • 53
0

Your problem is that you are calling the properties on the relative layout as if though you are declaring it again.

When you declare an ID for a view then it should be done like this android:id="@+id/button" and when you want Android to position that particular view above or below any other view in a relative layout you have to call it like this android:layout_below="@id/textview"

This tells android that you declared a button with id button and want it to be positioned below the textview, remember do not use @+id to postion view use @id instead.

Arif Nadeem
  • 8,524
  • 7
  • 47
  • 78
  • See my comment above. Thanks! – gordonwd Feb 19 '12 at 23:51
  • 1
    this is not entirely correct. if you have an view or viewgroup declared above another one in the xml but want to use for exampl toRightOf or toLeftOf, you can reference the view below using @+id and then use @+id again or @id when setting the id of the view/viewgroup itself. @+id will add the id to your R and then plug into the view it relates too when the app is built. – speedynomads May 30 '13 at 13:32
  • @domji84 You are wrong, you cannot reference Views or for the matter of fact any resource by using @+id, its sole purpose is to create new resource. See this answer http://stackoverflow.com/a/5025971/892055 – Arif Nadeem May 30 '13 at 17:30
  • 1
    the system handles it for you. if you want to address and xml view that is declared further down you page, @+id certainly does add the id to R for you. if you have layout_below="@+id/button2" in button 1 which is declared above button 2 and then set id="@+id/button2" on button 2, the layout_below will still work and button 2 will still have the id button2. Here is some example code that works fine even though @+id is used more that once to for button2. http://pastebin.com/aYsRCSWu – speedynomads May 31 '13 at 15:24