Open top menu



In this tutorial explain how to used expendable listView in android.

main.xml

In this class drag and drop expendableListView widget.
..............................................................................................................................................................

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#4079d6" >

            <ExpandableListView
                android:id="@+id/lvExp"
                android:layout_height="match_parent"
                android:layout_width="match_parent"/>

</LinearLayout>


list_group.xml

the another group item header xml class.
.................................................................................................................................................................

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp"
    android:background="#000000">


    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textSize="17dp"
        android:textColor="#f9f93d" />

</LinearLayout>



list_item.xml

create a another listview item xml class, child items.
.................................................................................................................................................

<?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="55dip"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17dip"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

</LinearLayout>


ExpendableAdapter.java

we will create a expendableAdapter class and extends BaseExpandableListAdapter  adapter

getGroupView()  the Method set the required group header.
getChildView() the method set listView item.

............................................................................................................................................................

 package com.example.androidbeginner;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpendableAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpendableAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

public Object getChild(int groupPosition, int childPosititon) {
// TODO Auto-generated method stub
return _listDataChild.get(this._listDataHeader.get(groupPosition))
        .get(childPosititon);
}

public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}

public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
}

public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
        .size();
}

public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return this._listDataHeader.get(groupPosition);
}

public int getGroupCount() {
// TODO Auto-generated method stub
return this._listDataHeader.size();
}

public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
       if (convertView == null) {
           LayoutInflater infalInflater = (LayoutInflater) this._context
                   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           convertView = infalInflater.inflate(R.layout.list_group, null);
       }

       TextView lblListHeader = (TextView) convertView
               .findViewById(R.id.lblListHeader);
       lblListHeader.setTypeface(null, Typeface.BOLD);
       lblListHeader.setText(headerTitle);

       return convertView;
}

public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}

   
}


MainActivity.java

.........................................................................................................................................................


package com.example.androidbeginner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class MainActivity extends Activity {

    ExpendableAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpendableAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);
        expListView.setOnGroupClickListener(new OnGroupClickListener() {

public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO Auto-generated method stub
return false;
}
});

        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();
}
});
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

public void onGroupCollapse(int groupPosition) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
                       listDataHeader.get(groupPosition) + " Collapsed",
                       Toast.LENGTH_SHORT).show();
}
});
        expListView.setOnChildClickListener(new OnChildClickListener() {

public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
 Toast.makeText(
                       getApplicationContext(),
                       listDataHeader.get(groupPosition)
                               + " : "
                               + listDataChild.get(
                                       listDataHeader.get(groupPosition)).get(
                                       childPosition), Toast.LENGTH_SHORT)
                       .show();
return false;
}
});
     
     
    }

    /*
     * Preparing the list data
     */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Top Most Used Color Name");
        listDataHeader.add("Planet Order List");
        listDataHeader.add("Coming Soon..");

        // Adding child data
        List<String> ColorName = new ArrayList<String>();
        ColorName.add("RED");
        ColorName.add("BLUE");
        ColorName.add("YELLOW");
        ColorName.add("GREEN");
        ColorName.add("PINK");
        ColorName.add("BLACK");
        ColorName.add("WHITE");

        List<String> nowShowing = new ArrayList<String>();
        nowShowing.add(" Sun");
        nowShowing.add("Mercury");
        nowShowing.add("Earth");
        nowShowing.add("Mars");
        nowShowing.add("Jupiter");    
        nowShowing.add(" Saturn");
        nowShowing.add("Uranus");
        nowShowing.add("Neptune");
       

        List<String> comingSoon = new ArrayList<String>();
        comingSoon.add("Aries - Your identity ");
        comingSoon.add("Taurus - Your personal resources");
        comingSoon.add("Gemini - Your communicative skills ");
        comingSoon.add("Cancer - Your family and home");
        comingSoon.add("Leo - Your interests and likings ");
     
        comingSoon.add("Virgo - Your day to day works ");
        comingSoon.add("Libra - Your partners in life");
        comingSoon.add("Scorpio - that which cannot be controlled by you  ");
        comingSoon.add("Sagittarius - your travels");
        comingSoon.add("Capricorn - your career and social status ");
     
        comingSoon.add("Aquarius - Your ideals in life ");
        comingSoon.add("Pisces - Your shortcomings ");
     

        listDataChild.put(listDataHeader.get(0), ColorName); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);
    }
}

0 comments