Add a header/footer in a listview in xml

No, I don't think that it is possible. Based on ListView source code there are only overScrollHeader/overScrollFooter are available from XML attributes. But these attributes accept only drawables.

If you don't want to use tricks with layouts above/below ListView. You can extend ListView and implement your own footer and header support in customized View. It is not so hard because of footer and header are already implemented. You only have to add XML attributes parsing in your customized View's constructor.


I was just trying to achieve the same thing (to keep my code cleaner and use XML for markup & source code for logic), but the only solution I found is to define the header view with XML somewhere in your layout and then detach it and put into ListView as header.

For example, having this XML:

<ListView android:id="@+id/myListView">
</ListView>

<LinearLayout android:id="@+id/myHeader">
    ....
</LinearLayout>

You can do this in your code:

ListView myListView = (ListView) findViewById(R.id.myListView);
LinearLayout myHeader = (LinearLayout) findViewById(R.id.myHeader);

// Let's remove the myHeader view from it's current child...
((ViewGroup) myHeader.getParent()).removeView(myHeader);

// ... and put it inside ListView.
myListView.addFooterView(myHeader);

Basically, what we do here is just detach the inflated LinearLayout from its parent and set it as ListView header child.

This is not an ideal solution, but it is still easier than creating/inflating header manually. Also this utilizes the power of XML inflation & view reusing if you're using this inside some "holder" pattern.

Hope this helps somebody.