VOOZH about

URL: https://www.geeksforgeeks.org/android/simpleexpandablelistadapter-in-android-with-example/

⇱ SimpleExpandableListAdapter in Android with Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

SimpleExpandableListAdapter in Android with Example

Last Updated : 15 Jul, 2025

Android ExpandableListView is a view that shows items as a vertically scrolling two-level list. The basic difference with ListView is that it allows two levels of the display, which can be easily expanded and collapsed by touching to view and their respective children's items. To show the view, ExpandableListViewAdapter is used in Android. In many apps, an ExpandableListView facility is required. For example:

  • In a "city" app(for any city), if the user wants to see a list of engineering colleges/list of art colleges/list of medical colleges, etc.,
  • List of vegetables/List of fruits/List of Nuts etc., for a "jiomart" kind of app
  • List of Hatchbacks/List of crosscuts/List of Sedans etc., for an "Uber" kind of app

Important Methods

Methods

Description

setChildIndicator(Drawable)

Current state indicator for  each item If the child is the last child for a group, the state state_last will be set

setGroupIndicator(Drawable)

To represent the state either expanded or collapsed.state_expanded is the state if the group is expanded, state_collapsed if the state of the group is collapsed, state_empty if there are no groups.

getGroupView()Used to get  the view for the list group header
getChildView()Used to get the view for list child item

The Notable Interfaces

Interfaces

Description

ExpandableListView.OnChildClickListenerWhen a child in the expanded list is clicked, this is overridden
ExpandableListView.OnGroupClickListenerWhen a group header in the expanded list is clicked, this is overridden
ExpandableListView.OnGroupCollapseListenerWhen a group is collapsed, this method notifies
ExpandableListView.OnGroupExpandListenerWhen a group is expanded, this method notifies


Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Note that select Java/Kotlin as the programming language.

Directory Structure for the Project:

👁 Directory_Structure


Step 2: Working with the XML files

  • To design the UI, code is present under the res\layout folder in the form of XML. They are used in the Activity files and once the XML file is in scope inside the activity, one can access the components present in the XML. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

activity_main.xml:

Design UI:

👁 Layout_1


Go to app > res > layout > New > Layout Resource File and name the file list_group. Below is the code for the list_group.xml file.

list_group.xml:

Next is for layout row for child items. This is done via a list_item.xml file. Go to app > res > layout > New > Layout Resource File and name the file as list_item. Below is the code for the list_item.xml file.

list_item.xml:


With the above XML, UI design elements are complete. Next is, a java file to populate the contents of the list.


Step 3: Working with the Java/Kotlin files

Go to app > java > your package name > Right-click > New > Java/Kotlin Class and name the file ExpandableListDataItems. Below is the code for the ExpandableListDataItems file. Comments are added inside the code to understand the code in more detail.

ExpandableListDataItems File:


Go to app > java > your package name > Right-click > New > Java/Kotlin Class and name the file CustomizedExpandableListAdapter. Below is the code for the CustomizedExpandableListAdapter file. This java class extends BaseExpandableListAdapter and it overrides the methods that are required for the ExpandableListView. Comments are added inside the code to understand the code in more detail.

Note: To learn more about BaseExpandableListAdapter refer to : BaseExpandableListAdapter in Android with Example

CustomizedExandableListAdapter File:

 

Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.

MainActivity File:

Output:

Conclusion

ExpandableListView is a very useful mandatory feature used in many apps. In mobile app sizes and the available space, to show multiple items, one should need features like ExpandableListView and ExpandableListAdapter so the view can fit perfectly. As the scrolling is available, we can keep information on many levels. The methods support expanding the header, collapsing the header, and selecting the child items perfectly as seen in the emulator output. For simplicity, we have provided Toast messages. Depending upon the requirements, we can able to add further coding to match it.

Comment
Article Tags:

Explore