Aligning fragments with RelativeLayout

android, android-fragments, android-layout, android-linearlayout, android-relativelayout

Solution

You can't use weights with a `RelativeLayout`. Basically that attribute gets ignored, meaning both your fragments render with a width of `0`, hence they are not visible.

I'm not sure what you're trying to accomplish, but you might want to consider wrapping your first example (the `LinearLayout`) into a `RelativeLayout` - in other words: combine/integrate both layouts. That does result in another level in your view hierarchy though.

Edit:

I haven't actually tested it, but I reckon something like this is what you're looking for:

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

    <LinearLayout 
        android:id="@+id/fragment_layout"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_above="@+id/footer_view" 
        android:weightSum="3">

        <fragment 
            android:id="@+id/fragmentTitles" 
            android:name="com.fragment.test.TitlesFragment"
            android:layout_width="0dp" 
            android:layout_height="fill_parent"
            android:layout_weight="1" />

        <FrameLayout 
            android:id="@+id/details"
            android:layout_width="0dp" 
            android:layout_height="fill_parent"
            android:layout_weight="2" />
    </LinearLayout>

    <TextView 
        android:id="@+id/footer_view" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true"
        android:background="#f00" 
        android:text="I am a footer" />

</RelativeLayout>

Obviously you can replace the `TextView` with anything you like.

Problem

I'm having real problems trying to align fragments with a `RelativeLayout`, though it seems this should be straightforward. I just need two fragments next to each other and if I use a `LinearLayout` it works fine: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.fragment.test.TitlesFragment" android:id="@+id/fragmentTitles" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" /> <FrameLayout android:id="@+id/details" android:layout_weight="1" android:layout_width="0px" android:layout_height="fill_parent" /> </LinearLayout> ``` However, if I use a `RelativeLayout`, nothing shows: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.fragment.test.TitlesFragment" android:id="@+id/fragmentTitles" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" /> <FrameLayout android:id="@+id/details" android:layout_weight="1" android:layout_width="0px" android:layout_height="fill_parent" android:layout_toRightOf="@id/fragmentTitles" /> </RelativeLayout> ``` Update: Here is a screenshot of what I'm seeing: This is the code I'm using: ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent" android:weightSum="3" > <fragment android:name="com.fragment1" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_above="@id/statusUpdated" /> <fragment android:name="com.fragment2" android:id="@+id/fragment2" android:layout_width="0px" android:layout_weight="2" android:layout_height="fill_parent" android:layout_above="@id/statusUpdated" android:layout_toRightOf="@id/fragment1" /> </LinearLayout> <TextView android:id="@+id/statusUpdated" style="@style/Status" /> </RelativeLayout> ```

Original source