I know how to add a header or a footer in JAVA, but I was wondering if I could add it straight in the XML. I wouldn't want to simulate this, but really add it as footer or header!
Android App Development for Beginners This example demonstrates how do I add footer in Android ListView. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Most printed publications use page headers and footers for almost all of their pages. To define them, you will use the page margin boxes. @page :first { @top-center { content: "How to Grow Flowers"; font-size: smaller; color: gray; } } You can set content and style to multiple page margin boxes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With