ListView takes up all space in LinearLayout
Asked Answered
S

1

5

I have this LinearLayout containing a ListView and a LinearLayout:

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

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:hint="Hej"/>
    </LinearLayout>
</LinearLayout>

The ListView takes up the entire layout and the LinearLayout with EditText gets added below the bottom edge of the screen and isn't visible. How can I fix this? I tried with layout_weight but didn't work.

Sharenshargel answered 18/12, 2015 at 15:19 Comment(2)
By the way, match_parent and fill_parent do the exact same thing. No need to use them both. And in fact, you should only be using match_parent because fill_parent was deprecated in API 8.Gaylagayle
@Gaylagayle Thanks for the clarification, I thought they were different.Sharenshargel
S
8

That is because you are using fill_parent, and it will do exactly that.

You could try something along the lines of this... It will cause the ListView to expand to fill the space.

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

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="Hej"/>
    </LinearLayout>
</LinearLayout>

Another alternative is to use a RelativeLayout. So long as the height of the ListView is not wrap_content, it should be OK.

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

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_above="@+id/footer"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="10dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="Hej"/>
    </LinearLayout>
</RelativeLayout>
Seize answered 18/12, 2015 at 15:21 Comment(4)
Thanks, I thought it would fill as much as it could and not do exactly like match_parent. I can accept in 10 minSharenshargel
fill_parent is deprecated, and equivalent to match_parent, use the later instead.Smollett
Perhaps thats why fill_parent is deprecated because its misleading :)Sharenshargel
Never gave much thought about that. Makes sense!Smollett

© 2022 - 2024 — McMap. All rights reserved.