Android Expandable ListView Tutorial: Step-by-Step Guide
Coding Tutorials

Expandable List View in Android

Vidhi Markhedkar
This is a tutorial based on the Android expandable list view which has the capability to group items and when the user clicks on the group it expands showing items. It can be useful to display a grouped set of items.

ExpandableListAdapter

It loads the data into the items associated with this view.

Some Methods used by this class:-

  • (Constructor) ExpandableListAdapter(Context context,List listDataHeader,HashMap> listHashMap -> Contains context of MainActivity class, List of Headers of each group and list of items of each group.
  • getgroupCount() -> returns the size of group list(header list).
  • getChildrenCount() -> returns the size of items of group whose groupPosition is given.
  • getGroup() -> returns the name of group whose groupPosition is given.
  • getChild() -> returns the name of item (whose childPosition is given)of a group( group whose groupPosition is given)
  • getGroupId() -> returns the group ID.
  • getChildId()-> returns child position.
  • isChildSelectable-> returns true as we want child to be selectable.
  • getGroupView() -> returns the view for list group header.
  • getChildView()-> returns the view for child items.

Some interfaces can be implemented by this class:-

  • OnChildClickListener: When a child in the expanded list is clicked, this method is invoked.
  • OnGroupClickListener: When a group header in the expanded list is clicked, this method is invoked.
  • OnGroupCollapseListener: Used to notify the user when a group is collapsed
  • OnGroupExpandListener: Used to notify the user when a group is expanded.

Resource files:-

We have a total of 3 resource files-

  • Activity_main.xml– to create a view of the main interface of the application.
  • list_group.xml-Layout file to create the header of each group.
  • list_item.xml-Layout file to create a list of items of each group.

Read More : Retrieve Image from Firebase Storage in Android

Activity_main.xml




    

    

Create two layout resource files list_group.xml and list_item.xml

Expandable List View 2
Step 1
Expandable List View 1
Step 2

List_group.xml




    


List_item.xml




    

MainActivity.java

package com.example.expandable_listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;

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

public class MainActivity extends AppCompatActivity {

    private ExpandableListView listView;
    private ExpandableListAdapter listAdapter;

//We need a list to create headers/titles of each group and a HashMap to handle item list of each group which is basically a pointer of items to the header of that class.
    private List listdataheader;
    private HashMap> listHashMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView=findViewById(R.id.explv);
        initData();
        listAdapter=new ExpandableListAdapter(this,listdataheader,listHashMap);
        listView.setAdapter(listAdapter);
    }

    private void initData()
    {
        listdataheader=new ArrayList<>();
        listHashMap=new HashMap<>();
        listdataheader.add("Heading1");
        listdataheader.add("Heading2");
        listdataheader.add("Heading3");
        listdataheader.add("Heading4");

        List  ed= new ArrayList<>();
        ed.add("Example of expandable listview");

        List android=new ArrayList<>();
        android.add("SubHeading1");
        android.add("SubHeading2");
        android.add("SubHeading3");
        android.add("SubHeading4");

       List android1=new ArrayList<>();
        android1.add("SubHeading1");
        android1.add("SubHeading2");
        android1.add("SubHeading3");
        android1.add("SubHeading4");

        List  ed1= new ArrayList<>();
        ed1.add("This is expandable listview");

        listHashMap.put(listdataheader.get(0),ed);
        listHashMap.put(listdataheader.get(1),android);
        listHashMap.put(listdataheader.get(2),android1);
        listHashMap.put(listdataheader.get(3),ed1);
    }
}

Create another class ExpandableListAdapter.

We can always set items to Expandable ListView using Adapter or List Adapter.

ExpandableListAdapter.java

package com.example.expandable_listview;

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;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List listDataHeader;
    private HashMap> listHashMap;

    public ExpandableListAdapter(Context context,List listDataHeader,HashMap> listHashMap)
    {
        this.context=context;
        this.listDataHeader=listDataHeader;
        this.listHashMap=listHashMap;
    }
    @Override
    public int getGroupCount() {
        return listDataHeader.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return listHashMap.get(listDataHeader.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return listDataHeader.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return listHashMap.get(listDataHeader.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String header=(String)getGroup(groupPosition);
        if(convertView==null)
        {
//it gives power to embed list_group view into our xml file.
            LayoutInflater inflater= (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.list_group,null);
        }
//fetch the parent value and set the value to Textview
        TextView lbiListHeader= (TextView)convertView.findViewById(R.id.lb1ListHeader);
        lbiListHeader.setTypeface(null, Typeface.BOLD);
        lbiListHeader.setText(header);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String childtext=(String)getChild(groupPosition,childPosition);
        if(convertView==null)
        {
            LayoutInflater inflater= (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.list_item,null);
        }
        TextView txtlistchild= (TextView)convertView.findViewById(R.id.lb1ListItem);
        txtlistchild.setText(childtext);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

After running your application, you will see your interface like this:-

This was a simple tutorial on the Expandable ListView of Android. I hope you like this article. For any Query, comment down below.

STAY TUNED FOR MORE SUCH ANDROID RELATED ARTICLES!!!!!!

Vidhi Markhedkar's profile picture

Vidhi Markhedkar

Android developer and writer with a passion for creating innovative and user-friendly apps.

Related Posts

7 Top Samsung Galaxy Ring Alternatives for 2025

7 Top Samsung Galaxy Ring Alternatives for 2025

Tired of waiting for the Samsung Galaxy Ring to hit the market? You’re not alone. While the buzz around Samsung’s smart ring continues, a plethora of impressive alternatives is already available. These devices deliver advanced health tracking, stylish designs, and unique features that cater to various lifestyles. The current lineup of smart rings caters to […]

What Is Quiet Mode on Instagram and How to Activate It

What Is Quiet Mode on Instagram and How to Activate It

Ever wondered what Quiet Mode on Instagram is all about? This simple yet powerful Instagram feature helps you take a break from the constant buzz of notifications and focus on what truly matters. Whether you’re striving for better work-life balance, dedicating time to studying, or simply trying to disconnect from social media distractions, Quiet Mode […]

How to Make a Bed in Minecraft (Step-by-Step Guide)

How to Make a Bed in Minecraft (Step-by-Step Guide)

A bed in Minecraft is very important. It lets you skip the night and set your spawn point, so if you die, you will return to your bed instead of the original world spawn. This guide will show you how to gather materials, craft a bed, and set your spawn point. We’ll also show you how to customize your bed, build bunk […]

10 Best MMORPG Games For Android

10 Best MMORPG Games For Android

Not too long ago, MMORPG games and powerful gaming consoles were mostly exclusive to PCs. They required high-end graphics and systems to deliver their immersive gameplay. But times have changed. With the rise of gaming-oriented smartphones, you can now enjoy PC-like gaming experiences on your Android device. Thanks to these technological advancements and faster internet […]

Roblox: Fruit Battlegrounds codes (January 2025)

Roblox: Fruit Battlegrounds codes (January 2025)

Fruit Battlegrounds codes are all about getting more gems and help you to shoot up your rank in this One Piece anime-inspired game. This Fruit Battlegrounds was made by Popo developer. It is an action-packed game where players battle it out using unique fruit-based abilities. With constant updates, new fruits, and exciting challenges, it’s a fruity frenzy you won’t […]

Roblox: Ultimate Football Codes (January 2025)

Roblox: Ultimate Football Codes (January 2025)

Want to get some extra items for Ultimate Football in Roblox? You’ve come to the right place! Here’s the latest list of codes to help you score touchdowns and look stylish on the field. These codes offer free rewards such as coins and cosmetics to enhance your gameplay. What are Ultimate Football Codes? Ultimate Football […]

Roblox: Da Hood Codes (January 2025)

Roblox: Da Hood Codes (January 2025)

Are you a fan of Roblox games, in this article we will look at the Roblox Da Hood Codes for December 2024 that will help you unlock exclusive items, improve your gameplay and dominate the streets of Da Hood. You can feel that the game is inspired by the Grand Theft Auto series and there […]