Kris B
Kris B

Reputation: 3578

Add view above and below a ListView

I'm all thumbs when it comes to working with Android layouts. I have a ListView and I need to add a TextView above and below a ListView. This is what I have so far:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
    >
    <!-- Header -->
    <RelativeLayout
        android:orientation="horizontal" 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        >
        <TextView 
            android:id="@+id/header" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
         />          
    </RelativeLayout>
    <!-- Header -->

    <LinearLayout
        android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
        android:padding="0dp"       
        >           
             <ListView 
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
            />

             <TextView android:id="@android:id/empty"
                android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                android:text="No items"
                android:padding="5dp"
                />
     </LinearLayout>

    <!-- Footer -->
    <RelativeLayout
        android:orientation="horizontal" 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        >
        <TextView 
            android:id="@+id/footer" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
         /> 
    </RelativeLayout>        
    <!-- Footer -->
  </LinearLayout>

Upvotes: 0

Views: 1847

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234867

In code, you could use addHeaderView and addFooterView to add the views to the ListView itself. In XML, you'll have to use a layout of some sort. A LinearLayout will do:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView 
        android:id="@+id/header" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />          

    <ListView 
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <TextView android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="No items"
        android:padding="5dp"
        />

    <TextView 
        android:id="@+id/footer" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        /> 
</LinearLayout>

Upvotes: 2

Related Questions