How to create custom listview with header to show about items hold text in this listview.
Header is used to show some one word or two word information about list view. Because when you have large amount of list view inside your project and you are showing multiple type of different different content in you app then header might help your users to understand what content you are showing in current listview. So with the help of another xml file and setting up that xml as header in listview using ViewGroup we can easily add header xml file in listview. So here is the complete step by step tutorial for Add/Set header to listview in android programmatically.
How to Add/Set header to listview in android programmatically.
Code for MainActivity.java file.
package com.setheadertolistview_android_examples.com;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView listView;
String[] listValue = new String[]
{
"ONE",
"TWO",
"THREE",
"FOUR",
"FIVE",
"SIX"
};
List<String> LISTSTRING;
LayoutInflater layoutinflater;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView1);
LISTSTRING = new ArrayList<String>(Arrays.asList(listValue));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, LISTSTRING);
layoutinflater = getLayoutInflater();
ViewGroup header = (ViewGroup)layoutinflater.inflate(R.layout.item_header,listView,false);
listView.addHeaderView(header);
listView.setAdapter(adapter);
}
}
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.setheadertolistview_android_examples.com.MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" > </ListView> </RelativeLayout>
Code for item_header.xml file.
<?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:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="ListView Header" android:textAppearance="?android:attr/textAppearanceLarge" android:background="#e900fe" android:textColor="#fefefe" android:padding="14dp" /> </LinearLayout>
Screenshot: