Android ExpandableListView - Androhub

Expandable ListView

Android ExpandableListView

Expandable list view is used to group list data by categories. It has the capability of expanding and collapsing the groups when user touches header.

If you are not aware of list view before please refer to this tutorial Android ListView Tutorial.

As seen in the Custom ListView we can use a type of array adapter to control the data displayed in the expandable list view. We will use the BaseExpandableListAdapter to supply and control the data displayed in this tutorial.

Example

In this example I will show you how to implement ExpandableListView.

VIDEO DEMO

Let’s get start by creating a project in Eclipse IDE.

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java).

2.  Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i had taken one expandable listview.

3. For expandablelsitview header items create a new xml file naming header.xml  with TextView.

4. Now, for child items create a new xml file naming childs.xml  with TextView.

5. Create a new class naming ExpandableListAdapter.java for ExpandableListView adapter as we done in ListView. In this class we set the header and child items fro expandablelistview by inflating both header and child layout.

6. Now, come to MainActivity.java and add the following code. In this class we set the items to adapter and set it to expandablelistview and also we worked on some of its methods list expandgroup, collapsegroup, etc.

7. Now, run the app and you will get the output as shown in video.

Thanks. :)

 

15 Comments

Marco
Wednesday, October 14th, 2015

Hi, how do I implement the inflate method for different fragments/layouts when clicking on different child items?

Droid
Thursday, October 15th, 2015

Hi, sorry friend but i didn’t get your question properly. Can you explain it more?
Thanks.

madhav
Sunday, August 7th, 2016

Hello , can u tell me how can i set empty array for one parent item like no child for it and click on parent show toast for it.

Dr. Droid
Sunday, August 7th, 2016

Hi Madhav,

For this just pass empty array for parent for which you want no child. Then on click event of that parent check the children count by using method getChildrenCount, it will return you 1 or 0 not sure for empty child. On this basis you can show Toast for it.

Thanks

ankesh kumar
Thursday, March 16th, 2017

tutorial is very helpful . but i want to set the arrow at left side . please help thanks . thank u so much for such a good and great tutorials……..

Dr. Droid
Thursday, March 16th, 2017

Hi Ankesh Kumar,

To implement arrow at left side you have to replace 107-118 code lines from ExpandableListAdapter java class with below one:

if (isExpanded) {
header_text.setTypeface(null, Typeface.BOLD);
header_text.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_up, 0,
0, 0);
} else {
// If group is not expanded then change the text back into normal
// and change the icon

header_text.setTypeface(null, Typeface.NORMAL);
header_text.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_down, 0,
0, 0);
}

Thanks

Prabhu
Monday, April 10th, 2017

Is it Possible to add Fragments as a child in ExpandableListView ?
Urgent Please Reply!!!!

Dr. Droid
Monday, April 10th, 2017

Hi Prabhu,

Unfortunately you cannot use fragment in ListView, GridView, ExpandableListView and RecyclerView. if you want to use fragment layout that you can use by providing layout as we use in ListView for custom row layout.

Thanks

Mrittunjay
Tuesday, January 2nd, 2018

Please explain how to filter data with search view in top. only filter method
@Override
public boolean onClose() {
// TODO Auto-generated method stub
VisitListAdapter.filterData(“”);
return false;
}

@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
VisitListAdapter.filterData(query);
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
VisitListAdapter.filterData(Query);
return false;
}

Dr. Droid
Tuesday, January 2nd, 2018

Hi Mrittunjay,

Check this link to implement filter : https://www.androhub.com/android-adding-search-functionality-list/

Thanks

Kajal Mehta
Wednesday, July 4th, 2018

Hello
tutorial is very helpful . but i want to set data from the server. please help me

M Awais
Thursday, July 25th, 2019

Thanks for Nice tutorial. If i want to add an icon (download icon) with arrow, how can i do that.? kindly suggest any solution. Thank you 🙂

Dr. Droid
Friday, July 26th, 2019

Hi M Awais,

If you want to make any UI changes like adding an icon you can go to header.xml or child.xml to make any UI changes to Header Section or Child section respectively.

Thanks

Michael
Sunday, March 6th, 2022

Thank you for the tutorial. How can i use expandable listview with viewpager.

I mean, when i click on the first child it would of a fragment layout, i can now swipe from that first child to the last child without go back to the expandable listview.

Not just on the first child, i want to swipe from any child i clicked from the expandable listview.

Thank you.

Dr. Droid
Monday, March 21st, 2022

Hi Michael,

I think I get confused what exactly the issue you are facing.
Is it possible to explain it a little bit more or you can mail me and attach some rough/mock-up design for reference.

Thanks

Post comment

Your email address will not be published. Required fields are marked *