Alexander Farber
Alexander Farber

Reputation: 22988

List in ListActivity occupies whole screen, pushing custom buttons outside

In a ListActivity I had to hardcode a height of 300dp for my main list, because it would otherwise occupy the whole screen and push my 2 buttons (show and cancel) at the bottom out of the screen:

<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"
    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="de.afarber.mynotification.MainActivity" >

    <ListView
        android:id="@android:id/list"
        android:dividerHeight="1px"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

    <TextView 
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0000"
        android:text="@string/no_beacons_found" />

    <Button android:id="@+id/show" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_margin="20dp"
        android:onClick="showNotification"
        android:text="@string/show_notification" />

    <Button android:id="@+id/cancel" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_margin="20dp"
        android:onClick="cancelNotification"
        android:text="@string/cancel_notification" />    

</LinearLayout>

This results in the following screenshot:

screenshot

But I am still not happy with it, because I would like to have the 2 buttons aligned at the bottom of the screen. And I would like the list to occupy the rest of the screen above the buttons. Finally I wonder how to place the empty text properly.

Please recommend me what to do, which layout to use in this case.

Upvotes: 0

Views: 70

Answers (2)

Kelevandos
Kelevandos

Reputation: 7082

Try this. What I did was put all the buttons into another layout, which should align to bottom of the page. Then I told the list to position itself above that layout with layout_above

<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"
                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="de.afarber.mynotification.MainActivity">

    <ListView
            android:id="@android:id/list"
            android:dividerHeight="1px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/buttons"
            />

    <LinearLayout
            android:id="@+id/buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical">

        <TextView
                android:id="@+id/emptyText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FF0000"/>

        <Button
                android:id="@+id/show"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:onClick="showNotification"
                android:text="TEXT"/>

        <Button
                android:id="@+id/cancel"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:onClick="cancelNotification"
                android:text="TEXT"/>

    </LinearLayout>

</RelativeLayout>

As for the empty text, simply skip the android:text="TEXT" in the declaration.

Upvotes: 1

Piotr Golinski
Piotr Golinski

Reputation: 1000

<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"
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="de.afarber.mynotification.MainActivity" >

<ListView
    android:id="@android:id/list"
    android:dividerHeight="1px"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp" />

<TextView 
    android:id="@android:id/empty"
    android:layout_width="match_parent"
     android:layout_weight="1"
    android:layout_height="0dp"
    android:background="#FF0000"
    android:text="@string/no_beacons_found" />

<Button android:id="@+id/show" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="20dp"
    android:onClick="showNotification"
    android:text="@string/show_notification" />

<Button android:id="@+id/cancel" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_margin="20dp"
    android:onClick="cancelNotification"
    android:text="@string/cancel_notification" />    

Upvotes: 0

Related Questions