Download Code

Android Expandablelistview Example

In android expandablelistview example you will learn about ExpandableListView.ExpandableListView is just like simple ListView.Mostly people says menu with submenu.


A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels. Groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.


An adapter that links a ExpandableListView with the underlying data. The implementation of this interface will provide access to the data of the children (categorized by groups), and also instantiate Views for children and groups.


Base class for a ExpandableListAdapter used to provide data and Views from some data to an expandable list view.

Create new android expandablelistview example application project.After that create three layouts.

ExpandableListView Layout(activity_main.xml).

android expandablelistview,android expandablelistview example,expandablelistview example,

Group Layout(group_item.xml).

android expandablelistview,android expandablelistview example,expandablelistview example,

Child Layout(child_item.xml).

android expandablelistview,android expandablelistview example,expandablelistview example,

Done with layouts.Now create new activity the following steps.

  • Extend this class with BaseExpandableListAdapter class.
  • Implement all unimplemented methods of BaseExpandableListAdaper class.
  • Create a constructor of ExpandableListAdapter class.
  • Create String List,Map to get groups and child.
  • Go to getChildView() and getGroupView() method and inflate layout of childs.

So you know to main modules of ExpandableListAdapter class.

package developer.expandlistview;

import java.util.List;
import java.util.Map;

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

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Activity context;
    private Map<String, List> developerCollections;
    private List groupHeader;
    public ExpandableListAdapter(Activity context, List groupHeader,
            Map<String, List> developerCollections) {
        this.context = context;
        this.developerCollections = developerCollections;
        this.groupHeader = groupHeader;
    public Object getChild(int groupPosition, int childPosition) {
        return developerCollections.get(groupHeader.get(groupPosition)).get(childPosition);
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        final String childItem = (String) getChild(groupPosition, childPosition);
        LayoutInflater inflater = context.getLayoutInflater();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.child_item, null);
        TextView item = (TextView) convertView.findViewById(;
        return convertView;
    public int getChildrenCount(int groupPosition) {
        return developerCollections.get(groupHeader.get(groupPosition)).size();
    public Object getGroup(int groupPosition) {
        return groupHeader.get(groupPosition);
    public int getGroupCount() {
        return groupHeader.size();
    public long getGroupId(int groupPosition) {
        return groupPosition;
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String laptopName = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
            convertView = infalInflater.inflate(R.layout.group_item,
        TextView item = (TextView) convertView.findViewById(;
        item.setTypeface(null, Typeface.BOLD);
        return convertView;
    public boolean hasStableIds() {
        return true;
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;



Now go to class.Follow the steps.

  • Create string List,Map variables.
  • Create variable for ExpandableListView.
  • Refer xml ExpandableListView.
  • Create group and childern.
  • Set ExpandableListAdapter with group and children.
  • Finally call setAdapter to set list to ExpandableListView.

package developer.expandlistview;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

	List groupList;
	List childList;
	Map<String, List> codecCollection;
	ExpandableListView expListView;

	protected void onCreate(Bundle savedInstanceState) {



		expListView = (ExpandableListView) findViewById(;
		final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(this, groupList, codecCollection);


		expListView.setOnChildClickListener(new OnChildClickListener() {

			public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
				final String selected = (String) expListAdapter.getChild(groupPosition, childPosition);
				Toast.makeText(getBaseContext(), selected, Toast.LENGTH_LONG).show();

				return true;

	private void createGroupList() {
		groupList = new ArrayList();

	private void createCollection() {
		// preparing laptops collection(child)
		String[] language = { "C++", "Java","PHP" };
		String[] ide = { "Eclipse", "Xamarin", "Notepad++" };
		String[] movie = { "ABC", "ABX","X Series", "Z Series" };

		codecCollection = new LinkedHashMap<String, List>();

		for (String groupItem : groupList) {
			if (groupItem.equals("Language")) {
			} else if (groupItem.equals("IDE"))

			codecCollection.put(groupItem, childList);

	private void loadChild(String[] groupChild) {
		childList = new ArrayList();
		for (String add_items : groupChild)

	private void setGroupIndicatorToRight() {
		/* Get the screen width */
		DisplayMetrics dm = new DisplayMetrics();
		int width = dm.widthPixels;

		expListView.setIndicatorBounds(width - getDipsFromPixel(35), width
				- getDipsFromPixel(5));

	// Convert pixel to dip
	public int getDipsFromPixel(float pixels) {
		// Get the screen's density scale
		final float scale = getResources().getDisplayMetrics().density;
		// Convert the dps to pixels, based on density scale
		return (int) (pixels * scale + 0.5f);


Run your android expandablelistview example application project.

android expandablelistview,android expandablelistview example,expandablelistview example,

that’s it for this tutorial.