Search code examples
androidandroid-layoutandroid-constraintlayoutandroid-relativelayout

Fill with Layout until next layout (at bottom)


So, I have a header, and then a CardView that should fill the screen until the footer. Problem is, it fills all the screen, and the footer goes "under" the screen, as you can see in the images.

I want to keep the footer (button + the two images) always at the bottom and the cardview should always fill the rest of the screen. There is a scrollview in the cardview that will allow text to be read in the cardview.

Anyway, here is the XML code:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_10">

    <View
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:background="@color/colorPrimary" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:layout_marginBottom="@dimen/spacing_mlarge"
        android:orientation="vertical"
        android:padding="@dimen/spacing_mlarge">

    <TextView
        android:id="@+id/qstView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_marginBottom="20dp"
        android:text="Insert question text here"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textColor="@color/white" />

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

        <android.support.v7.widget.CardView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="@dimen/spacing_middle"
            android:layout_marginEnd="@dimen/spacing_middle"
            android:layout_marginLeft="@dimen/spacing_middle"
            android:layout_marginRight="@dimen/spacing_middle"
            android:layout_marginStart="@dimen/spacing_middle"
            android:layout_marginTop="@dimen/spacing_middle"
            android:visibility="visible"
            app:cardCornerRadius="6dp"
            app:cardElevation="5dp">

            <ScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/checkbox_full_layout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_margin="5dp"
                        android:orientation="vertical">

                    </LinearLayout>

                </LinearLayout>
            </ScrollView>

        </android.support.v7.widget.CardView>
    </LinearLayout>
    <Button
        android:id="@+id/add_field_button"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:onClick="onAddAnswer"
        android:layout_gravity="bottom"
        android:layout_marginLeft="-4dp"
        android:layout_marginRight="-4dp"
        android:background="@drawable/btn_rounded_primary"
        android:text="Suivant"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:textStyle="bold" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="@dimen/spacing_mlarge"
        android:layout_gravity="center_horizontal"
        android:gravity="center">
        <ImageView
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="@dimen/spacing_mlarge"
            android:layout_marginTop="@dimen/spacing_large"
            android:src="@drawable/ineedhelp"/>
        <View
            android:layout_width="@dimen/spacing_large"
            android:layout_height="0dp" />
        <ImageView
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="@dimen/spacing_mlarge"
            android:layout_marginTop="@dimen/spacing_large"
            android:src="@drawable/mbtouch_dark"/>
    </LinearLayout>
        <View
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/spacing_middle"
            android:layout_alignParentBottom="true"/>
    </LinearLayout>
</RelativeLayout>

This is what I have at the moment:

Relative Layout - I have at the moment

And this is what I would like to have (here I forced dimensions, but since not all devices are the same, the space will not be used optimally).

Relative Layout - I want

Thanks in advance!


Solution

  • Try to arrange your view like the manner below (For CardView and bottom layout, rest things will stay as they were)..

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        ....
        ...>
    
         <!--  Since, weight is 1 it will occupy whole space subtracting height of bottom  -->
         <CardView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            .... />
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ....>
    
            <!--  Put your bottom widgets over here  -->
    
        </LinearLayout>
    
    </LinearLayout>