Search code examples
androidandroid-activityandroid-linearlayout

Android LinearLayout fill until bottom of screen


I have an Android activity that is composed by: FrameLayout > ScrollView > LinearLayout > ListView

My ListView is my last component and I would like that its height size would go from the ending of the previous component up to the bottom of the screen. Problem: unfortunately right now its size is only the height size is only the size of 1 row.

I have set all layout_height to match_parent property, but no effect.

Question: would you know how to make my ListView occupy all the height space down to the bottom of the available screen?

Thank you!

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="it.bitrack.fabio.bitrack.ScheduleView"
    android:layout_marginTop="60dp">

    <!-- TODO: Update blank fragment layout -->

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

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

            <TextView
                android:id="@+id/textView7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="Select an asset"
                android:textStyle="bold|italic" />

            <Spinner
                android:id="@+id/assetSpinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:textStyle="bold|italic" />

            <LinearLayout
                android:id="@+id/assetBookingLinearLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView9"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Booking schedule"
                    android:textStyle="bold|italic" />

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

                    <TextView
                        android:id="@+id/textView12"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        android:text="From: "
                        android:textStyle="bold|italic" />

                    <TextView
                        android:id="@+id/fromDatetimeTextView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="5dp"
                        android:text="Date and time" />

                    <ImageButton
                        android:id="@+id/setFromDatetimeImageButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="5dp"
                        android:src="@android:drawable/ic_menu_my_calendar" />

                    <TextView
                        android:id="@+id/textView13"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="10dp"
                        android:text="To: "
                        android:textStyle="bold|italic" />

                    <TextView
                        android:id="@+id/toDatetimeTextView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="5dp"
                        android:text="Date and time" />

                    <ImageButton
                        android:id="@+id/setToDatetimeImageButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="5dp"
                        android:src="@android:drawable/ic_menu_my_calendar" />
                </LinearLayout>

                <Button
                    android:id="@+id/assetBookingButton"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Proceed with booking" />

            </LinearLayout>


            <LinearLayout
                android:id="@+id/assetBookingsLinearLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView8"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Current bookings"
                    android:textStyle="bold|italic" />

                <ListView
                    android:id="@+id/assetBookingsListView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>

        </LinearLayout>>
    </ScrollView>

</FrameLayout>

Solution

  • use this layout code:

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp">
    
        <!-- TODO: Update blank fragment layout -->
    
        <ScrollView
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Select an asset"
                    android:textStyle="bold|italic" />
    
                <Spinner
                    android:id="@+id/assetSpinner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:textStyle="bold|italic" />
    
                <LinearLayout
                    android:id="@+id/assetBookingLinearLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
    
                    <TextView
                        android:id="@+id/textView9"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        android:text="Booking schedule"
                        android:textStyle="bold|italic" />
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">
    
                        <TextView
                            android:id="@+id/textView12"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:padding="10dp"
                            android:text="From: "
                            android:textStyle="bold|italic" />
    
                        <TextView
                            android:id="@+id/fromDatetimeTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:padding="5dp"
                            android:text="Date and time" />
    
                        <ImageButton
                            android:id="@+id/setFromDatetimeImageButton"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:padding="5dp"
                            android:src="@android:drawable/ic_menu_my_calendar" />
    
                        <TextView
                            android:id="@+id/textView13"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:padding="10dp"
                            android:text="To: "
                            android:textStyle="bold|italic" />
    
                        <TextView
                            android:id="@+id/toDatetimeTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:padding="5dp"
                            android:text="Date and time" />
    
                        <ImageButton
                            android:id="@+id/setToDatetimeImageButton"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:padding="5dp"
                            android:src="@android:drawable/ic_menu_my_calendar" />
                    </LinearLayout>
    
                    <Button
                        android:id="@+id/assetBookingButton"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Proceed with booking" />
    
                </LinearLayout>
    
            </LinearLayout>
        </ScrollView>
    
        <LinearLayout
            android:id="@+id/assetBookingsLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/scrollview"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/textView8"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="Current bookings"
                android:textStyle="bold|italic" />
    
            <ListView
                android:id="@+id/assetBookingsListView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    
    </RelativeLayout>
    

    Couple of suggestion:

    1. Dont's use listview inside ScrollView...
    2. use RelativeLayout as a root element to align views one after another.
    3. For scrollView use android:layout_height="wrap_content" and android:fillViewport="true"

    Output:

    enter image description here