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.

Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • 1
    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. – NoChinDeluxe Dec 18 '15 at 15:22
  • 1
    @NoChinDeluxe Thanks for the clarification, I thought they were different. – Arbitur Dec 18 '15 at 15:24

1 Answers1

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>
Community
  • 1
  • 1
Knossos
  • 15,802
  • 10
  • 54
  • 91