Miguel Borges
Miguel Borges

Reputation: 7659

listview with header and footer

I am trying to build an Android View, with a listview as a header and footer. so far only managed that only appears to the header. Here is a picture that explains what I want and that I've got.

Attempt

Does anyone know what I'm doing wrong? Dry the current code:

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff">

<!--  Header Starts-->
<LinearLayout android:id="@+id/header" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="@layout/header_gradient" 
        android:layout_margin="5dip" 
        android:paddingTop="13dip" 
        android:paddingBottom="8dip">
    <!-- Logo Start-->
    <ImageView android:src="@drawable/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" />
    <!-- Logo Ends -->
</LinearLayout>
<!--  Header Ends -->

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

        <LinearLayout android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dip">

            <EditText android:id="@+id/searchText"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"/>

            <ImageButton android:id="@+id/searchButton"
                    android:layout_height="wrap_content" 
                    android:layout_width="wrap_content" 
                    android:src="@android:drawable/ic_menu_search" />
        </LinearLayout> 

        <ListView android:id="@+id/lstNews" 
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip" />
    </LinearLayout>

<!-- Footer Start -->
<LinearLayout android:id="@+id/footer" 
     android:layout_width="fill_parent" 
     android:layout_height="30dip" 
     android:background="@layout/footer_gradient" 
     android:layout_margin="5dip" 
     android:paddingTop="5dip" 
     android:paddingLeft="5dip" 
     android:layout_alignParentBottom="true">
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:text="Instituto Superior de " />
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:textStyle="bold" android:text="Engenharia" />
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:text=" do Porto" />
</LinearLayout>
<!-- Footer Ends --></LinearLayout>

Upvotes: 5

Views: 1814

Answers (3)

Roman Black
Roman Black

Reputation: 3497

In the second root layout set height wrap_content or anything else but not fill_parent. If Yuo want to set wrap_content set android:weight 1.0 or simple 1

Upvotes: 0

silleknarf
silleknarf

Reputation: 1219

One option is to use a RelativeLayout instead.

Firstly you will need to change your outermost LinearLayout to a RelativeLayout.

The next step is to add the property android:layout_above="@id/content" to the header.

<!--  Header Starts-->
<LinearLayout android:id="@+id/header" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@layout/header_gradient" 
    android:layout_margin="5dip" 
    android:paddingTop="13dip" 
    android:paddingBottom="8dip"
    android:layout_above="@id/content">

The final step is to add the properties android:layout_above="@id/footer" and android:id="@+id/content" to the LinearLayout that contains the content and the listview i.e.

<!-- Content -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
          android:id="@+id/content"
          android:layout_height="fill_parent"
          android:layout_width="fill_parent"
          android:orientation="vertical"
          android:gravity="center"
          android:layout_above="@id/footer">

Upvotes: 3

HannahMitt
HannahMitt

Reputation: 1020

Items are added to the LinearLayout in order, so when the ListView fills parent it takes up all the space. I'd try setting the layout_weight of the ListView to 1.

Upvotes: 0

Related Questions