Hi all……

Here is a simple example of expandandable ListView in ANDROID.
But I am not going to explain any code, because everything is explained inside the java file.
Make sure to read it.

Expandable ListView

package pack.Coderzheaven;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

public class ExpandableListDemo extends ExpandableListActivity {

	public void onCreate(Bundle savedInstanceState) {

        SimpleExpandableListAdapter expListAdapter =
			new SimpleExpandableListAdapter(
					createGroupList(), 				// Creating group List.
					R.layout.group_row,				// Group item layout XML.
					new String[] { "Group Item" },	// the key of group item.
					new int[] { R.id.row_name },	// ID of each group item.-Data under the key goes into this TextView.
					createChildList(),				// childData describes second-level entries.
					R.layout.child_row,				// Layout for sub-level entries(second level).
					new String[] {"Sub Item"},		// Keys in childData maps to display.
					new int[] { R.id.grp_child}		// Data under the keys above go into these TextViews.
			setListAdapter( expListAdapter );		// setting the adapter in the list.

    	}catch(Exception e){
    		System.out.println("Errrr +++ " + e.getMessage());

	/* Creating the Hashmap for the row */
	private List createGroupList() {
	  	  ArrayList result = new ArrayList();
	  	  for( int i = 0 ; i < 15 ; ++i ) { // 15 groups........
	  		HashMap m = new HashMap();
	  	    m.put( "Group Item","Group Item " + i ); // the key and it's value.
	  		result.add( m );
	  	  return (List)result;

	/* creatin the HashMap for the children */
	private List createChildList() {

    	ArrayList result = new ArrayList();
    	for( int i = 0 ; i < 15 ; ++i ) { // this -15 is the number of groups(Here it's fifteen)
    	  /* each group need each HashMap-Here for each group we have 3 subgroups */
    	  ArrayList secList = new ArrayList();
    	  for( int n = 0 ; n < 3 ; n++ ) {
    	    HashMap child = new HashMap();
    		child.put( "Sub Item", "Sub Item " + n );
    		secList.add( child );
    	 result.add( secList );
    	return result;
    public void  onContentChanged  () {
    /* This function is called on each child click */
    public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
    	System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);
    	return true;

    /* This function is called on expansion of the group */
    public void  onGroupExpand  (int groupPosition) {
    		 System.out.println("Group exapanding Listener => groupPosition = " + groupPosition);
    	}catch(Exception e){
    		System.out.println(" groupPosition Errrr +++ " + e.getMessage());

The main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 <ExpandableListView android:id="@+id/android:list"

     <TextView android:id="@+id/android:empty"
               android:text="No items"/>

The child_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    <TextView android:id="@+id/grp_child"


The group_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    <TextView android:id="@+id/row_name"


The strings.xml (This file contains the string for the color that is used for text in the ListView)

<?xml version="1.0" encoding="utf-8"?>
    <string name="app_name">ExpandableList Demo</string>
    <drawable name="white">#ffffff</drawable>
	<drawable name="blue">#2554C7</drawable>
	<drawable name="green">#347C2C</drawable>
	<drawable name="orange">#ff9900</drawable>
	<drawable name="pink">#FF00FF</drawable>
	<drawable name="violet">#a020f0</drawable>
	<drawable name="gray">#778899</drawable>
	<drawable name="red">#C11B17</drawable>

The manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ExpandableListDemo"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

Please leave your comments if the post was useful.
Follow me on facebook, twitter and google plus for more updates.




  1. Haresh Makadiya April 15, 2011 at 9:45 am

    can u send me code plz…

    • James April 17, 2011 at 6:03 am

      Haresh we have send you the code.. If you didn’t get it, please leave a comment here.

      • Ben October 16, 2011 at 12:39 pm

        Could you send me the code?

  2. James April 15, 2011 at 9:54 am

    Hello Haresh…..
    You can just make new project and copy and paste the code into their respective files.
    I have given the file names to copy on top of each code also.
    Additionally you need only an image named “bkg.png” or “bkg.jpg” in your drawable folder. that’s all……..
    If Still you couldn’t get it. Kindly leave a comment. We are here to help you.

  3. JS April 15, 2011 at 1:25 pm

    Great tutorial! I ‘m interested in making a n level tree any suggestions on how to do that?

    • James April 17, 2011 at 5:54 am

      Hi JS….
      I think you can return another expandable Listview to create an n level tree in the following function by extending BaseExpandableListAdapter class..

      public View getGroupView(int groupPosition, boolean isExpanded,
      View convertView, ViewGroup parent)

  4. rajesh April 21, 2011 at 7:25 am

    you created single row listview…for that you created the chaild list view…….but my problem is i want to make main listview with two rows…..how can i plz????

    • James April 21, 2011 at 10:24 am

      @rajesh : i have explained this in one of the comments above (in reply to JS). Currently I am little busy in my company project. So I can’t produce a sample for you now.

  5. chandu April 25, 2011 at 7:51 am

    i need a two row(two rows text in group list) expandable list view if psbl pls help me…….

  6. Aagrah April 25, 2011 at 8:40 am

    Hii Haresh,
    Is there any way to customize the parent row of the list, I mean the row which is opening the child row(Group Item 1)?

    • James April 25, 2011 at 8:45 pm

      if you want to customize group, then change the color or something in group_row.xml

  7. John April 25, 2011 at 5:11 pm

    Which API are you compiling against? I tried this with two emulator instances, one running Android 2.2 and the other with the Google APIs for 2.2 (both level 8). When loaded, all I get is a blank screen 🙁

    • James April 25, 2011 at 8:27 pm

      It will work with all API’s. I was using ANDROID 2.0 when I wrote it. Check the Logcat if you got any errors or not. Check whether you have copied everything perfectly. Drag and copy the code, don’t double click and copy.

  8. wang_peng1 May 4, 2011 at 12:17 pm

    can yousend me your code or give me a link about the download it

    • James May 4, 2011 at 8:23 pm

      Hello wang_peng1, We have sent you the code. Please check your mail and leave us a comment.

  9. Cannon May 9, 2011 at 8:01 pm

    You seems to be an expert in this field, good article and keep up the good work, my buddy recommended me this.

    My blog:
    Meilleur Taux aussi Rachat de Credit immobilier

  10. ishan May 16, 2011 at 11:31 am

    icant make it correnctly. igot an error
    can u please send me the code please >>

  11. ishan May 17, 2011 at 10:53 am

    thankz u very much..i got the project.and it works .u seems to be very help full. i have lot of problems with android..how can i ask .. post a comment or somethig else .please tell me.. 😀

    • James May 17, 2011 at 10:55 pm

      Hi ishan,
      Post your doubts as comments….If we have time we will solve your problem for sure…..

  12. ali June 15, 2011 at 6:35 pm

    hi, please send me the complete code . thanks

  13. Ali June 16, 2011 at 1:18 pm

    Hi, i want to know that this is expandable list view or Custom view? in the application.


    I want to make some application with this much good look.


  14. Joint pain relief June 22, 2011 at 10:55 pm

    I just signed up to your blogs rss feed. Will you post more on this subject?

    • James June 23, 2011 at 6:17 am

      Yeah sure……

  15. rohit June 24, 2011 at 11:25 am

    Hi, please mail the code

  16. Monster July 1, 2011 at 10:17 am

    Hi, please mail the code

    • James July 3, 2011 at 5:06 pm

      Please give a correct mail ID

  17. Padmanbhan July 1, 2011 at 2:02 pm

    Can we get dynamic data from database when we click the Group item.

    • James July 1, 2011 at 8:45 pm

      @padmanabhan : You can do whatever you like on group item click. There is a listener for group item in the program please check.

  18. july July 3, 2011 at 3:18 pm

    hi, please send me the complete code .
    Thanks in advance.

    • James July 3, 2011 at 5:19 pm

      July – Please check your mail, we have mailed you the code.

  19. Anna July 7, 2011 at 8:23 pm

    hi, please send me the complete code .
    Thanks in advance


    • James July 7, 2011 at 8:26 pm

      Hello Anna,
      Please check your mail we have send you the whole project.

  20. Anna July 7, 2011 at 8:30 pm

    thanks for such a quick reply:)

    • James July 7, 2011 at 8:31 pm

      Always welcome. keep in touch.

  21. Jitendra July 9, 2011 at 11:41 am

    Can we fix the parent in expandable listview. Please give me any suggestion if we can’t.

    • James July 11, 2011 at 9:56 pm

      What are you trying to say? Can you please explain.

      • Jitendra July 12, 2011 at 1:03 pm

        When we scroll the expandable list view then parent also scroll with child. I want to fix parent when we scroll child.

  22. Neha July 15, 2011 at 3:11 pm


    Can you tell me how to drag the child items in list2, if we have xml which is divided into 2 lists, list1 & list2.We are displaying expandable list in list1 & want to drag the child items in list2.

    Please tell me if u know.


    • James July 15, 2011 at 10:24 pm

      Sorry Neha, Not sure.. I have to work it out.

  23. Praveen August 1, 2011 at 12:18 pm

    Hello, Can you send me the whole code. praveen2288@gmail.com

    • James August 1, 2011 at 9:05 pm

      Hello Praveen:- Please check your mail. We have sent you the code.

  24. Gabriela August 8, 2011 at 10:52 am

    Is the code public? If so, can you please send me the source code? Thx either way, have a nice day.

    • James August 8, 2011 at 8:05 pm

      Hi Gabriela, Please check your mail.

  25. Jegadeesh August 13, 2011 at 11:33 am

    Please send me the complete code to jegadjame@gmail.com.

    Thanks in advance

  26. sujs August 17, 2011 at 4:39 am

    Hi James,
    Can you please send me the code. How to get dynamic data onclick of group item, can u please give an hint.

    Thank you.

  27. Roman August 24, 2011 at 2:41 pm


    thank you for this example. I am currently trying to use the ExpandableListView in an Activity which does not extends ExpandableListActivity. I want to create a Layout which is divided in two LinearLayouts. The inner one holds some textViews and images. And the outer one holds the ExpandableListView.

    The application has one “StartActivity” but even before i open the Activity with the ExpandableListView the application crashes.

    Do you have some simple code or tutorial for this? Would be great. Thanks in advance.

    • James August 24, 2011 at 9:15 pm

      Hello Roman :- Check your inbox. we have send you the code for Expandable Lists.

  28. Kfir August 25, 2011 at 6:29 pm


    I’m also like to user an ExpandableListView in an Activity which is not ExpandableListActivity. I’m trying to create a layout which have expandable list view which act as multiple options list right beside regular button which suppose to start a test.
    How can I do that?
    Thanks in advanced.

    • James August 25, 2011 at 9:22 pm

      You can have an activity which expands ExpandableListActivity and add other views like buttons in that. No Problem.

  29. Jyothi September 2, 2011 at 10:03 am

    How to show main items following subitems automatically (without clicking main item) using listview in android? If I use ExpandableListActivity, I need to click on the main item to see the subitems. I want to show mainitem following subitems at one instance(click evnt is not neede.When I run my application, I want both main item and subitems must be displayed on the emulator. how can i achieve this using listview? Please help me.

    • James September 3, 2011 at 11:07 am

      Hello Jyothi.
      The use some custom Layout. you dont need ExpandableList.

  30. chris September 22, 2011 at 2:07 am

    Wow this is awesome,
    Can you please send me the full source code? have a nice day.

    How to use ExpandableListActivity to open another activity?
    Please help me.

    • James September 27, 2011 at 10:40 pm

      Just call startActivity and pass your activity name.

  31. Ivan September 28, 2011 at 12:23 pm


    Thanks for the post, could you please send me the source code. I like to try it out

  32. HT September 29, 2011 at 3:04 am

    Good one, pls share the code. Thanks in advance.

  33. Aric Michael Warden October 1, 2011 at 2:25 pm

    Exactly what I looking for…thanks
    A little late…but could I get source code too?

  34. Rusty October 4, 2011 at 5:01 pm

    Great post!.could you please send me the source code? Thx

  35. kent October 11, 2011 at 1:42 pm

    this tutorial realy helps my project but i’m having problem putting images in group and child rows… id like to have little thumbnails beside the button.. can you please send me some samples?? and i samples that group and child are manualy made.. i cant edit it since it’s looped.. thanks in advance. please reply.. my deadline is friday

    • James October 11, 2011 at 8:31 pm

      Hey Kent please check your email.

  36. Monika October 19, 2011 at 6:56 pm

    Could u send me the code please !!!

  37. Dc October 21, 2011 at 3:17 am

    Tyvm for the useful posts,
    however it is not working on my computer, it suns fine and it says it was successful but i dont see screen like yours, all i see is my bkg pic, any idea?
    thx in advance

    • James October 21, 2011 at 10:30 pm

      Check your inbox. we have sent you the code.

  38. Mufaddal October 24, 2011 at 1:01 pm

    Can you please send me the code?


  39. rizaozdulger October 26, 2011 at 5:19 pm


    Thanks to the post.

    Can you send me the code please?

    Thanks again!

  40. Max November 2, 2011 at 9:49 pm

    Can I get the code as well? Thanks!

    • James November 2, 2011 at 11:26 pm

      Where to send the code?

  41. Sowjanya November 5, 2011 at 12:34 am

    Can you send me the code?

  42. Seshu November 9, 2011 at 11:41 am

    Hi. Good tutorial.. can u send the code to my mail. this is my mail id s.seshu143attherate gmaildotcom.. thank Q.

  43. Kaushal November 11, 2011 at 6:29 pm


    This is really good tutorial and very helpful.

    Can anyone have an idea to hide the default expand/collapse icon?

    Thanks in advance,

  44. Kumar November 11, 2011 at 6:58 pm

    Thanks to the post,
    Can you send me the code please?


  45. Mufaddal November 11, 2011 at 7:58 pm


    How do we make multicolumn expandablelistview.

    I added the things to the hashmap and also to the XML but no luck like the way it is described for ListView.


    • James November 11, 2011 at 9:34 pm

      What do you mean by multicolumn? You can have any custom layout for the row.

    • Thomas November 13, 2011 at 3:18 pm

      I think I know what you’re trying to do. I couldn’t find any documentation on doing this either, but I just kept trying different things and figured this out. My code is based on the code on this page so you should be able to figure it out.

      First the java code:

      //for each slqite column this create 4 subrow columns where my database column names are Item, QuantitySold, SoldPrice and SetPrice. This is done right before returning the child list.

      for( int n = 0 ; n < mycursor.getCount(); n++ ) {

      HashMap child = new HashMap();

      child.put( "Sub Item", mycursor.getString(mycursor.getColumnIndex("Item")) );

      child.put( "Sub Item1", mycursor.getString(mycursor.getColumnIndex("QuantitySold")) );

      child.put( "Sub Item2", mycursor.getString(mycursor.getColumnIndex("SoldPrice")) );

      child.put( "Sub Item3", mycursor.getString(mycursor.getColumnIndex("SetPrice")) );
      secList.add( child );
      result.add( secList );
      return result;
      And here is my adapter formatting:

      mAdapter = new SimpleExpandableListAdapter(this, createGroupList(),R .layout.expandable_list_item1,new String[] { "Group Item" }, new int[] { R.id.toprow_name }, createChildList(), R.layout.expandable_list_item2, new String[] {"Sub Item", "Sub Item1", "Sub Item2", "Sub Item3"}, new int[] { R.id.subrow_item, R.id.subrow_quantitysold, R.id.subrow_minprice, R.id.subrow_setprice});

      Second my xml layout code (this is crap formatting, its 2:50am and I just figured this out so I havn't written in proper xml format).

      That should do it!

      • Thomas November 13, 2011 at 3:25 pm

        Oops, the website grabbed my xml code. I’ll try pasting it again, but basically its just 4 textviews with the id’s I mentioned above (subrow_item, subrow_quantitysold…) with a linear layout that is set to horizontal. I specify the textview width=20dp, but height is set to wrap_content.

  46. Andik November 16, 2011 at 11:44 pm

    Hi… nice tutorial…
    I’ve searched so many tutorials and examples about usability of expandablelistview, but I think this is the most obvious I’ve ever read…
    May I have the code please… can you send me the complete code via my email: top_x_classix@yahoo.com


  47. Umesh November 18, 2011 at 3:39 pm

    Thanks for the very nice tutorial.
    Your code is so simple to understand.
    great job…keep it up

    • James November 18, 2011 at 7:39 pm

      Thanks Umesh for the encouragement.

  48. Umesh November 18, 2011 at 3:42 pm

    Hey Andik,you can get the code from the link here………

  49. Carlos December 9, 2011 at 6:59 am

    Hi, the tutorial is great, I got a fight of two days with tutorials lists and more lists. Now I have a couple of questions that I’m fighting with your code.
    I can not find how to change the bottom of each row according to a condition such as: if (a == 2) row.setBackgroundDrawable (R.drawable.team) elserow.setBackgroundDrawable (R.drawable.only).
    The other is that I need to put the first subitem is fixed, ie
    Item description: blabla n No. tests n Rating: bla bla
    item team1
    item team2

    the explanation is that I have the title of the race that is first rate, once deployed
    I have the description of the race so many items and then another level as teams.

    Sorry for the long text Thanks

  50. sandeep dheringe December 22, 2011 at 10:58 pm

    awesome code….. very userful. thanks.

  51. Irfan Khan January 6, 2012 at 12:34 pm

    Great tutorail.Thanks alot for such a nice tutorial

  52. vicky January 7, 2012 at 6:27 am

    hey..I’ve downloaded the project…but i got error s on the expandablelist.java…do i need to change anything or add anything to the code to make it working?


  53. vicky January 7, 2012 at 7:19 am

    OH.. it work..thank you so much for the codes.

  54. Saumya January 11, 2012 at 8:07 am

    Hi… Am retrieving data as string from data base to android like name ,address, phone. This is dynamic pull from database i want it to display on android in a list but the activity should not extend ListAcitvity as I do have edit text on same screen. I want to display the name on list and when clicked rest of the details should come , am not able to display the list pls do help me .

    • James January 11, 2012 at 8:41 pm

      You can simply change the ListActivity and change the way ListView is referenced. If it is a ListActivity the ListView in the xml has the id = “@android:list” or something. You can simply change this to give ur id and change the way adapter is setup. thats all.

  55. mafia wars January 17, 2012 at 9:34 am

    I’ll right away snatch your rss as I can not to find your email subscription hyperlink or newsletter service. Do you have any? Please allow me recognise in order that I may subscribe. Thanks.

  56. playfred January 21, 2012 at 6:14 am

    Helpful information. Fortunate me I found your web site by chance, and I’m stunned why this coincidence did not happened in advance! I bookmarked it.

  57. Greg February 6, 2012 at 11:30 am

    Great tutorial, thanks! Most tutorials always leave out something critical like the XML file.

  58. meena February 10, 2012 at 4:20 pm

    This is very helpful for me. because i am new in android development. i am developing Android Digital menu system application.i want one help.
    how to put icons in front of the expanablelist view and how to view the list items like image format…….. please give the sample program…….
    Advance Thanks for our responce……

    • James February 10, 2012 at 7:21 pm

      Did you download the sample project I provided below the post, there you can change the adapter for the list.

  59. linhlt February 11, 2012 at 8:06 am

    I want to create a multi level list view. I just want to do a simple example about this. Just two class, one class extends Activity, one class extends BaseExpandableListAdapter. I want to know how to implement the class extends BaseExpandableListAdapter to return an expandable list view for multilevel

  60. linhlt February 11, 2012 at 8:07 am

    Please help me how to do that.

  61. Pingback: ListView with Sections in android. | Coderz Heaven

  62. Nash March 23, 2012 at 5:16 am

    Hi , Can you send me the code for the simple expandablelistview..thanks!

    • James March 23, 2012 at 7:35 pm

      The source code is below the post. PLease check that.

  63. Shankha Acharya April 9, 2012 at 11:27 am

    HI ,
    I great thanks for the tutorial. 🙂

  64. Pingback: ClassCastException: java.util.HashMap with android SimpleExpandableListAdapter | Software development support, software risk,bugs for bugs, risk analysis,

  65. jayesh patel April 13, 2012 at 8:16 am

    please send me source code of this application

    • admin April 13, 2012 at 1:14 pm

      hello jayesh patel :- You can download the project from the download link below.

  66. Rachelle April 15, 2012 at 5:10 am

    Awesome tutorial thanks!! anyone who uses this don’t forget to put the colours in the string.xml file (I’m not the best at reading and that cost me a lot of unnecessary debugging) 🙂

  67. Shrey April 16, 2012 at 7:07 am

    I found it very useful but only thing i found problem was with getting values at the position where i am clicking..

  68. Steve May 16, 2012 at 6:35 pm

    This has been a great help. The only thing I’m not seeing is how the adapter gets linked to the list view with the name you gave it.

  69. krishnaveni August 14, 2012 at 1:00 pm

    Hi the above tutorial is very helpful for me..thanks for giving this nice tutorial…but i have one doubts… how is creating 2 groups in this example for separate details.For eg:
    Group name:
    Childname(OrderInfo): payment_method
    Childname(CustomerInfo): name,email.
    How is to do.

  70. krishnaveni August 16, 2012 at 10:12 am

    Hi i have to created different groups in this example.but i can’t develop different child list is placed in different formats.please refer my code and give me solutions.now i have to created 2 groups in my code.
    I need to place “payment_method” and “total” inside first group,and firstname and lastname is place in second group
    My code can placed Childname(OrderInfo) inside OrderInfo group, but i wish to need if i have to click customerInfo group means the firstname,lastname also displayed customerInfo group.

    Here first child value is placed first group is successfully finished…but i can’t do place the second child value is placed second group.please help me.how is to do.
    The code is here:http://pastie.org/4520473

  71. Oshri Almakies August 23, 2012 at 5:20 pm

    can i have to the code please?

    • James August 24, 2012 at 3:49 am

      The full android project is below the post for download.

  72. GoGreen August 27, 2012 at 3:29 pm

    Nice one.

  73. Pingback: android expanlistview arrow : Android Community - For Application Development

  74. Pingback: android expandlistview arrow : Android Community - For Application Development

  75. Strangle faisal February 4, 2014 at 4:27 pm

    all solution about any software android app http://www.stranglefaisal.com


Leave a Reply

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




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.