Listview with header footer view android

At times we need to add some title to a list view or show some data at the very end of list view . For this we need not pass this data in adapter of listview , instead we can add it as header and views in the following way.

If you are not sure how to create a listview  you can refer these examples.

Step 1: Create a layout xml for the header and footer as per your requirement.

header.xml

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

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="HEADER"
    android:background="#cf9f99" />

</LinearLayout>

footer.xml

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

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="FOOTER"
    android:background="#cf9f99" />

</LinearLayout>

Step 2: Now let’s inflate these header and footer views and add it to our list view .

LayoutInflater inflater = getLayoutInflater();
		ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, listView,
				false);
		ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, listView,
				false);
		listView.addHeaderView(header, null, false);
		listView.addFooterView(footer, null, false);



Complete Code 

LayoutInflater inflater = getLayoutInflater();
		ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, listView,
				false);
		ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, listView,
				false);
		listView.addHeaderView(header, null, false);
		listView.addFooterView(footer, null, false);

Complete Code

public class MainActivity extends Activity {
	ListView listView;

	static final String[] numbers = new String[] { "one", "two", "three",
			"four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
			"twelve", "thirteen", "fourteen", "fifteen", "sixteen",
			"seventeen", "eighteen", "nineteen", "twenty", "twenty one",
			"twenty two" };
	View header;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) findViewById(R.id.list_id);

		//code to add header and footer to listview
		LayoutInflater inflater = getLayoutInflater();
		ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, listView,
				false);
		ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, listView,
				false);
		listView.addHeaderView(header, null, false);
		listView.addFooterView(footer, null, false);

		ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1, numbers);

		listView.setAdapter(adapter);

	}

}

Screenshots:

footerList                        headerList

Download this example form here .

Related Links:

GridView Array Adapter Example

ListView ArrayAdapter Example

Custom ListView Example

Custom Grid View Example

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s