This tutorial is about the expandable listview in Android which helps to display data in a group. You can create multiple listviews in your activity which can be expanded on the click event. The main idea of an Android Expandable listview is that it has groups and every group has at least one child. Groups can be expanded and shrunk by clicking on them.

The Expandable List view is mainly used to display data/options in groups. The main difference between an ordinary listview and an expandable listview is that the expandable listview allows two levels, groups and children (groups are individually expanded).

Below you can find the screenshot of the Android expandable listview application. We are displaying three lists inside our main activity.


Implementing an Android Expandable ListView

We need to implement two java classes, one for our MainActivity class and a second for implementing an adapter for an expandable listview.

Follow the steps given below to implement an Android expandable listview in your application:

  1. Header for an expandable listview
    We have multiple listviews in our application and all of them are expandable.  We create an arraylist for each group and it will hold its child items. So we need to create one array list which will contain a list of all headers/groups.
    Snippet 1:
  2. Child Items for an Expandable listview
    Each header/group has child items associated with it. These list items can be hidden or shown depending upon the click event. So we need to create one more arraylist which will hold the items for an Android expandable listview.
    Snippet 2:
  3. Now we need to associate the child items with the group view. For that we will create a new HashMap.
    Snippet 3:
  4.  Next, we will implement our own custom adapter for an expandable listview.  The code of the adapter class can be seen here after the MainActivity class.
    Snippet 4:
  5. You can also add event listeners to an Android expandable listview. The complete code to implement the MainActivity for an Android expandable listview can be seen below.

Listing 1: 
Create the new file

So now we are done with the MainActivity of our Android expandable listview application. Now we are left with an adapter for our expandable listview. But before implementing an adapter, let’s add the necessary layout file. Let’s create the XML layout files for our listview application.

Listing 2 : Create the file activity_main.xml which contains the main layout for an expandable listview.

The layout for the group/header element

An Expandable listview contains one group/header and multiple children. So let’s define the layout of the header(group). In our application, it would be a text box with a black background.

Listing 3 : Create the file list_group_header.xml

The layout for the child elements

Listing 4 : Create the file list_sub_child.xml

Adapter for expandable list

Next we will implement an adapter for the expandable listview.

Listing 5 : Create the file


This was all about the expandable listview in Android. If you have any suggestions or received any errors while implementing this procedure, feel free to comment below.