Monday, May 2, 2011

Android ListViews

I've been working on an Android ListActivity which needs to have two TextViews anchored to the bottom of the screen. The anchoring was fairly simple, but when the list extended to the bottom of the screen, it obscured the anchored TextViews.

The solution is to declare the ListView last in your xml layout file, and specify what you want to be above it and what you want to be below it. I.e.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android
    ....>

    <TextView
        android:id="@+id/my_screen_header"
        .......>
    </TextView>

    <RelativeLayout
        android:id="@+id/my_screen_footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <TextView
            android:id="@+id/first_footer_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp">
        </TextView>
        <TextView
            android:id="@+id/second_footer_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/first_footer_text_view"
            android:textSize="15sp">
        </TextView>
    </RelativeLayout>
    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/my_screen_footer"
        android:layout_below="@id/my_screen_header"/>
</RelativeLayout>

Thanks to http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/

No comments:

Post a Comment