21

Can someone explain to me why the ImageView is not appearing above the LinearLayout?

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/rev_main"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <!-- some stuff in here -->
    </LinearLayout>
    <ImageView
        android:id="@+id/rev_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow"
        android:layout_above="@id/rev_main"
        />
</RelativeLayout>

I don't get it.

Onik
  • 19,396
  • 14
  • 68
  • 91
Andrew
  • 20,756
  • 32
  • 99
  • 177
  • Does it appear anywhere? nowhere at all? maybe post a pic of how it looks and it it would be easier to tell why. In my Layouts I use IDs like "@+id/rev_main" Even in the attributes like layout_above. Truthfully though Im not even sure of the difference between the two, perhaps try that? – FoamyGuy May 11 '11 at 21:27
  • It disappears. If I declare the ImageView first and try to make the LinearLayout position itself above the ImageView, the LinearLayout disappears as well. – Andrew May 11 '11 at 22:03
  • Just FYI - had this problem inside listview item layout. After I applied fixed height for item layout, the problem went away. – Hossain Khan Jul 12 '16 at 18:29

4 Answers4

27

This happens when you specify alignment relative to another layout. The solution I found was to go the other direction.

Instead of telling the ImageView to be above the LinearLayout, tell the LinearLayout to be below the ImageView.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/rev_main"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rev_arrow">
        <!-- some stuff in here -->
    </LinearLayout>
    <ImageView
        android:id="@+id/rev_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow"
        />
</RelativeLayout>
Error 454
  • 7,255
  • 2
  • 33
  • 48
  • 1
    Here's my problem with this: The drawable used for the ImageView changes. Which means I need a variable for it in code. The position (above or below) the LinearLayout also changes. If I need to edit the LayoutParams for the LinearLayout, that means I also need a variable for the LinearLayout. I was hoping to just need a variable for the ImageView. – Andrew May 11 '11 at 21:52
  • 1
    Well, variables are cheap. Alternatively, you could create two image views, 1 above & 1 below and set the initial state to android:visibility="gone". Then you could programatically change the visibility state of each view based on your needs. – Error 454 May 11 '11 at 22:05
  • Don't you also need to move the rev_arrow ImageView above the LinearLayout in the xml? Otherwise you'll get a reference error since the file is parsed linearly. I just ran into that issue. :-P – stuckj Apr 08 '15 at 21:14
  • I have one view positioned aligned to some other view and want to have some other view above it: I have to use `layout_above`. Solved it by putting the two to a `LinearLayout` – Oliv Oct 13 '15 at 07:00
7

The following should work for you:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/rev_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow"
        android:layout_alignParentTop="true"
        />
    <LinearLayout
        android:id="@+id/rev_main"
        android:layout_width="fill_parent"
        android:layout_below="@id/rev_arrow"
        android:layout_height="wrap_content">
        <!-- some stuff in here -->
    </LinearLayout>
</RelativeLayout>
Joseph Earl
  • 23,351
  • 11
  • 76
  • 89
  • 2
    I have a problem with this. Sometimes I want the LinearLayout above the ImageView. When I change the attribute on the LinearLayout to layout_above, the LinearLayout disappears entirely. – Andrew May 11 '11 at 22:01
1

I had an similar problem by creating a custom optionsMenu. The simplest way to set z-order of views was to do it programmatically. If you want to switch the order sometimes, your should easily call:

    ImageView yourImageView = (ImageView)findViewById(R.id.rev_arrow);
    yourImageView.bringToFront();

I don´t know if it is adaptive to your application, but in my case it works perfect. If you need more code, let me know.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
0

If you have such problem in ListView make sure that you use proper inflation method. Parent view group must be specified for correct inflation.

mLayoutInflater.inflate(R.layout.listitem, parent, false);

Don't use

mLayoutInflater.inflate(R.layout.listitem, null);
Oleksii Masnyi
  • 2,922
  • 2
  • 22
  • 27