how to Split ListView with Sections in Android

In this blog post, I’ll show how to Split ListView with Sections in Android.

1. Create a new project File -> Android Project. While creating a new project give activity name as and copy this code:

import java.util.ArrayList;
import java.util.TreeSet;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

    private MyAdapter mAdapter;
    String[] numNames = { "Zero", "One", "Two", "Three", "Four", "Five", "Six",
            "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
            "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
            "Ninteen", "Twenty" };

    public void onCreate(Bundle savedInstanceState) {
        mAdapter = new MyAdapter();
        for (int i = 0; i < numNames.length; i++) {

            if (i == 0) {
                mAdapter.addSeparatorItem("Single Digit");
            if (i == 10) {
                mAdapter.addSeparatorItem("Double Digit");
            mAdapter.addItem(numNames[i] + "     " + i);

    // Adapter Class
    private class MyAdapter extends BaseAdapter {

        private static final int TYPE_ITEM = 0;
        private static final int TYPE_SEPARATOR = 1;
        private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

        private ArrayList<String> mData = new ArrayList<String>();
        private LayoutInflater mInflater;

        private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

        public MyAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        public void addItem(final String item) {

        public void addSeparatorItem(final String item) {
            // save separator position
            mSeparatorsSet.add(mData.size() - 1);

        public int getItemViewType(int position) {
            return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR
                    : TYPE_ITEM;

        public int getViewTypeCount() {
            return TYPE_MAX_COUNT;

        public int getCount() {
            return mData.size();

        public String getItem(int position) {
            return mData.get(position);

        public long getItemId(int position) {
            return position;

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            int type = getItemViewType(position);
            if (convertView == null) {
                holder = new ViewHolder();
                switch (type) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.section1, null);
                    holder.textView = (TextView) convertView
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.section2, null);
                    holder.textView = (TextView) convertView
            } else {
                holder = (ViewHolder) convertView.getTag();
            return convertView;


    public static class ViewHolder {
        public TextView textView;

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == {
            return true;
        return super.onOptionsItemSelected(item);

Step 2: Open your AndroidManifest.xml and copy paste this code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
android:versionName="1.0" >

android:targetSdkVersion="19" />

android:theme="@style/AppTheme" >
android:label="@string/app_name" >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />




Hope this helpful. Your valuable comments are always welcomed. It will help to improve my post and understanding.
Download Source Code from here.

durga chiranjeevi

durga chiranjeevi

I'm currently working as Android developer at Energy Alternatives India. Interested in Application and Game development.
durga chiranjeevi

Leave a Reply

Your email address will not be published. Required fields are marked *