Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView takes up all space in LinearLayout

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.

like image 275
Arbitur Avatar asked Dec 18 '15 15:12

Arbitur


1 Answers

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>
like image 127
Knossos Avatar answered Oct 23 '22 02:10

Knossos