Search code examples
javaandroidandroid-layoutandroid-fragmentsandroid-relativelayout

RelativeLayout - Keep Fragment at the bottom


This is my code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/buttonsFragment1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ScrollView
        android:id="@+id/scrollViewer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonsFragment1">

        <LinearLayout
            android:id="@+id/showSomething"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:orientation="vertical" />

    </ScrollView>

    <FrameLayout
        android:id="@+id/buttonsFragment2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/scrollViewer" />

</RelativeLayout>

The last FrameLayout contains a Fragment with some Buttons. This FrameLayout should be kept at the bottom of the screen. But with this code, the fragment buttonsFragment2 is not displayed on my Smartphone.

I see only the buttonsFragment1 on top, I can scroll down the LinearLayout but at the bottom, there is no buttonsFragment2. What is wrong?

EDIT: My Fragment class:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    View view = inflater.inflate(R.layout.myFragment, container, false);

    Fragment topFrag = new TopFragment();
    Fragment bottomFrag = new BottomFragment();

    FragmentTransaction top = getChildFragmentManager().beginTransaction();
    top.add(R.id.topFragment, topFragment).commit();

    FragmentTransaction bottom = getChildFragmentManager().beginTransaction();
    bottom.add(R.id.bottomFragment, bottomFragment).commit();

    return view;
}

Solution

  • If I understood your layout correctly, this is the solution you are searching. The scrollview fills the screen between your fragments:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout
            android:id="@+id/buttonsFragment1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <ScrollView
            android:id="@+id/scrollViewer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/buttonsFragment2"
            android:layout_below="@+id/buttonsFragment1">
    
            <LinearLayout
                android:id="@+id/showSomething"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:orientation="vertical" />
    
        </ScrollView>
    
        <FrameLayout
            android:id="@+id/buttonsFragment2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
    
    </RelativeLayout>