Android ExpandableListView

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. :)

Download Source Code – >

 

COMMENTS (4)
Reply

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

    Reply

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

Reply

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.

    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

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)