Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextViews on top and bottom, remaining space filled with ListView

I'm trying to put a TextView on top, another on bottom, and a ListView in between using the remaining space. However, my ListView is taking all the space to the bottom, making the last TextView to not even show up. The only way I can make it show up is to give the ListView a fixed height, like 100dp.

My layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/id_task_list_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_task_list"
        style="?android:attr/listSeparatorTextViewStyle" />

    <ListView
        android:id="@+id/id_task_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/id_task_list_title" />

    <TextView
        android:id="@+id/id_task_list_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/id_task_list_view"
        android:text="OMG Test" />

</RelativeLayout>
like image 628
Pedro Moreira Avatar asked Oct 23 '13 10:10

Pedro Moreira


2 Answers

Use LinearLayout as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context=".MainActivity"
                android:orientation="vertical">

    <TextView
            android:id="@+id/id_task_list_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/title_task_list"
            style="?android:attr/listSeparatorTextViewStyle" />

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

    <TextView
            android:id="@+id/id_task_list_test"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="OMG Test" />

</LinearLayout>
like image 164
Amulya Khare Avatar answered Nov 16 '22 17:11

Amulya Khare


This should solve your issue by placing your ListView between the TextViews using both android:layout_above and android:layout_below :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/id_task_list_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_task_list"
        style="?android:attr/listSeparatorTextViewStyle" />

    <TextView
        android:id="@+id/id_task_list_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="OMG Test" />

    <ListView
        android:id="@+id/id_task_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/id_task_list_test"
        android:layout_below="@id/id_task_list_title"/>


</RelativeLayout>
like image 31
2Dee Avatar answered Nov 16 '22 19:11

2Dee