Search code examples
androidandroid-linearlayoutandroid-relativelayout

Android LinearLayout background Image


I'm having a problem with my view if I try to display an image as background, it took the whole space and the second child of linear layout (2 RelativeLayout that appears next to each other) does not appear into view.

How can I set image as background keeping other layout elements above it ?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/globe"
        android:layout_gravity="top"
        />

    </RelativeLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >
        </RelativeLayout>


      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >
        </RelativeLayout>

    </LinearLayout>


</LinearLayout>

Solution

  • It is a bit late, but my problem was solved with the following code.

    Used the ImageView to set my background image and then displayed the linear layout over it.

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:contentDescription="@string/my_msg"
            android:src="@drawable/my_super_bg_image" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/top_style"
            android:orientation="vertical" >
    
            <RelativeLayout
                android:id="@+id/today"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingRight="5dp" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:text="some text here"
                    android:textSize="24sp" />
            </RelativeLayout>
    
        </LinearLayout>
    
    </FrameLayout>
    

    If some one needs to know more, I will update the answer.

    Thanks.