sree_iphonedev
sree_iphonedev

Reputation: 3534

How to fit a list view to the bottom in Android

I am new to Android, and I am facing many issues for layout. I have a screen with Toolbar on the top. Below that, I have an ImageView, and below the ImageView, a ListView. I am facing a problem with laying out the ListView height. Since my ListView is the last item on the screen, I would like it to fit with the remaining area of the screen. This, is I would like to resize the ListView dynamically so that it fits. Below is the layout that I set for the screen:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.team.sidhesh.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="617dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorAccent"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <ImageView
            android:id="@+id/sliderTempImageView"
            android:layout_width="match_parent"
            android:layout_height="@dimen/slider_image_height"
            android:src="@drawable/slider_image1"
            android:scaleType="centerCrop"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="@dimen/slider_seperator_height"
            android:src="@drawable/slider_seperator"
            android:scaleType="fitXY"/>

        <ListView
            android:id="@+id/list_view"
            android:background="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true">

        </ListView>

    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

Upvotes: 0

Views: 96

Answers (2)

Charuka Silva
Charuka Silva

Reputation: 13153

You can simply enclose your ListView inside a LinearLayout with height match_parent and setting gravity to bottom. Something like this,

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

    <ListView
        android:id="@+id/list_view"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Upvotes: 0

Sean Amos
Sean Amos

Reputation: 59

It depends on the type of your view's parent ViewGroup.

For RelativeLayout parents, Jeetendra Choudhary's answer of android:layout_alignParentBottom="true" will work fine.

For LinearLayout parents, you should have your view be the last child of the layout and set android:layout_weight="1" to have it fill the space to the bottom.

For FrameLayout parents, you should set android:layout_gravity="bottom" on your view.

You should not use an AppBarLayout as your root ViewGroup. I would recommend something like this, with a LinearLayout or FrameLayout as the root ViewGroup.

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorAccent"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <ImageView
            android:id="@+id/sliderTempImageView"
            android:layout_width="match_parent"
            android:layout_height="@dimen/slider_image_height"
            android:src="@drawable/slider_image1"
            android:scaleType="centerCrop"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="@dimen/slider_seperator_height"
            android:src="@drawable/slider_seperator"
            android:scaleType="fitXY"/>

    </android.support.design.widget.AppBarLayout>

    <ListView
        android:id="@+id/list_view"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Additionally, ListView is fairly dated. I would recommend looking into using RecyclerView from the support library instead. https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

Upvotes: 1

Related Questions