Android ListActivity with a header or footer
This post will show you how to go about implementing a ListActivity with a header or footer that does not follow the same layout as the list rows. A well known and obvious example of this is the main screen on the Android Market.
First, create a class that extends ListActivity:
ListWithHeader.java
package samcoles.listwithheader;
import android.app.ListActivity;
import android.os.Bundle;
public class ListWithHeader extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
}
}
Remember to ensure you have added this activity to your AndroidManifest.xml.
Next up you’ll need to create the list_main layout that’s set in the above ListActivity within /res/layout. Remember the <ListView> must have id=”@+id/android:list” and you should also have a view with id=”@+id/android:empty” to display when there’s no items in the list. Your ListActivity will go looking for these and your app will crash when loading this activity if you don’t have them. If you’re not aware of this then the debugger can be a bit unhelpful. The TextView uses a string resource, empty_list that you’ll need to remember to add too if you’re using this.
here’s the list_main layout:
list_main.xml
<?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="wrap_content" android:orientation="vertical"> <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="none"/> <TextView android:id="@+id/android:empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/empty_list" /> </LinearLayout>
You’ll also need a layout for the header, note that this below references a couple of string resources and an image resource:
list_header.xml
<?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="wrap_content"
android:id="@+id/header_layout_root"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:background="#000000"
android:textColor="#ffffff"
android:textSize="28dp"
android:gravity="center_horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp">
</TextView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/sample_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/sample_image"
android:scaleType="fitXY">
</ImageView>
<TextView
android:id="@+id/sample_title"
android:text="@string/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="12dp"
android:background="#AA000000"
android:padding="5dp"
android:layout_alignParentLeft="true"
android:layout_alignBottom="@id/sample_image">
</TextView>
</RelativeLayout>
</LinearLayout>
Next go back to your ListActivity class and it’s time to put in the code to add the header to your list, place this just below the call to setContentView():
ListView lv = getListView(); LayoutInflater inflater = getLayoutInflater(); View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root)); lv.addHeaderView(header, null, false);
To explain the above, first we call getListView() to get the activity’s ListView and assign this to an object. We then call getLayoutInflater() and do the same – this gets the LayoutInflater that’s attached to the current application context. The LayoutInflater is then used to instantiate the layout xml file so that we have a View object that, in line 4, we can attach to the ListView! There is also a method ListView.addFooterView() – you use this in exactly the same way as ListView.addHeaderView().
The full ListActivity class is shown below, it also puts some items in the list.
ListWithHeader.java
package samcoles.listwithheader;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListWithHeader extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
//add header to list
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root));
lv.addHeaderView(header, null, false);
//add some list items
String listItems[] = {"List Item One", "List Item Two", "List Item Three", "List Item Four", "List Item Five"};
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems));
}
}
And here’s a look at that in the emulator:
This entry was posted by Sam Coles on March 17, 2011 at 9:16 pm, and is filed under Mobile. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site.

Thanks you for that code example
you make my day
Thank you guy!!you make my day too!!
Nice example! thank you!
but cant figure what is R.id.header_layout_root.
Going to search more…
it’s the id for the parent LinearLayout on the header layout, list_header.xml
Any idea how you could arrange these horizontally? ie imagine a landscape layout, with listview on left, image on right and title above?
Thanks,
Ben
Awesome Man!
How I do for stay my header static ??
Great post! Thank you!
After days of trial and error, this post finally helped me to find what I was looking for.
Is there anyway to add Header to Empty ListView ?
I’ve 3 listview in one activity ,
followers(3) , following(0) , latest post(9)
Header is not visible with following list , because it is empty .
//Add ListView Header
LayoutInflater inflater = getLayoutInflater();
final View header = inflater.inflate(R.layout.user_profile_header, (ViewGroup) findViewById(R.id.userProfileHeader));
displayListView.addHeaderView(header, null, false);
It’s help me a lot with a simple add header in android simple list view.
Thnku a lot.
Hi!
Nice stuff, but in MY case, when i run it with my data, the header disapears when scrolling the datarows.
It seems it is not a fixed header/footer, but just a kind of particular line of the list?
So is it really the case?
Or did I just miss something?
Thanks.
The write-up posted was quite informative and valuable.
You folks are performing an excellent job. Keep going.
Thanks a ton for the post!!
I was looking for a way to add a static row but adding it as header is way better . The post helped me solve my problem .:)