Android Listview with Header and Footer Buttons

Android listview with header and footer buttons

First create two layout file. like as footer_layout.xml & header_layout.xml and add footerview-headerview in list view

LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout listFooterView = (LinearLayout)inflater.inflate(
R.layout.footer_layout, null);

list.addFooterView(listFooterView);

LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.header_layout, null);

list.addHeaderView(listHeaderView);

Android ListView with fixed header and footer

I solved it by using @blackbelt suggestion and a small ImageView with the source image being transparant with a tile background.

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

<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@+id/tv_footer"
android:layout_below="@+id/tv_header" />

<TextView
android:id="@+id/tv_footer"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/footer_bg"
android:gravity="center"
android:text="Footer" />

<TextView
android:id="@+id/tv_header"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/header_bg"
android:gravity="center"
android:orientation="vertical"
android:text="Header" />

<ImageView
android:id="@+id/iconView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/lv"
android:background="@drawable/header_bg2"
android:src="@drawable/transparant_bg_tile" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/tv_footer"
android:layout_alignParentRight="true"
android:background="@drawable/footer_bg2"
android:src="@drawable/transparant_bg_tile" />

</RelativeLayout>

Screenshot from device
Sample Image

ListView with button at the bottom in Activity with fixed header and footer

The only way I could achieve the exact requirements I explained above was doing the following:

-In the XML file with the layout of the list items, I added a button with android:visibility="gone".

-In my custom ListView adapter, I always add an empty object in the last position. I simply add an object with a flag that shows it is the last in the list.

-In the method getView() that I override in the custom Adapter, I check if the object in the current position has the flag indicating that it is the last in the list. If so, I set the button visibility to VISIBLE, otherwise I fill the other components.

If anyone is facing the same problem and needs some sample code from my solution just ask here.

Add header and footer to list view without setting adapter

You won't be able to achieve that.

The way header and footer layouts work with ListView is the following: Whenever you add a header or footer to your ListView, it will cause your own adapter to be wrapped by a WrapperListAdapter and basically make your header and footer items be treated as list items in their own way(handled by the adapter of course). Thus, your ListView can't really know about these items if it has no adapter.

Android: Cannot get a clickable ListView header/footer

I see a few issues:

  • when inflating the header, use getListView() as the second parameter (root, where you have null now)l
  • should the header be a View or a ViewGroup? I've ended up using ViewGroup in these situations.
  • finally -- perhaps you should be setting the click listener on the button in the header instead of the header itself?

float header and footer in listview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/linearLayout1"
android:layout_below="@+id/tvListHeader"
android:orientation="vertical" >

<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_notes"/>
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal" >

<Button
android:id="@+id/bListBack"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Back" />
</LinearLayout>

<TextView
android:id="@+id/tvListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF0000"
android:textStyle="bold" />

android listviews: header and footer views

Why not just collapse the header and footer to zero height, or gray out the buttons (even better).

And the best user experience, in my opinion, would be to dynamically load more items when needed (i.e. upon scroll), like the built-in Gmail app does.

Android: Show listview between header and footer

Here is the tutorial links:

http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/

http://www.vogella.de/articles/AndroidListView/article.html

http://www.vogella.de/articles/AndroidListView/article.html#headerfooter



Related Topics



Leave a reply



Submit