Introduction

This tutorial explains Expandiblelistview.
ExpandileListView
Expandiblelistview is a View that shows items in a vertically scrolling two-level list. The difference between ListView and Expandiblelistview is that it contains two levels that can expand two show children of a group.
In this tutorial, we first extend the ExpandiblelistActivtiy class. Now we need an adapter like a list view to bind the data. So we use BaseExpandibleListAdapter that provides a child of a group when we expand it. Now first we need two layouts, one is to show a group row and another to show a child row in an expandible ListView.
In this class, we create a group and its child by using it in an ArrayList and it to the constructor of the Adapter class where it will use by the getChidview method and getGroupview method to be shown on the screen.
  1. ArrayList<String> groupItem = new ArrayList<String>();
  2. ArrayList<Object> childItem = new ArrayList<Object>();
  3. public void setGroupData()
  4. {
  5. groupItem.add("Android Version");
  6. groupItem.add("Iphone Version");
  7. }
  8. public void setChildItem() {
  9. ArrayList<String> child = new ArrayList<String>();
  10. child.add("CupCake");
  11. child.add("Donut");
  12. child.add("Eclair");
  13. child.add("Froyo");
  14. childItem.add(child);
  15. child = new ArrayList<String>();
  16. child.add("Iphone OS 1.X");
  17. child.add("Iphone OS 2.X");
  18. child.add("Iphone OS 3.X");
  19. child.add("Iphone OS 4.X");
  20. childItem.add(child);
  21. }
Step 1
Create an XML file Activitymain.xml and write this:
  1. <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
  2. android:id="@+id/textView1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="60dp" android:layout_marginLeft="5dp"
  5. android:gravity="center_vertical"
  6. android:textColor="#45454545"
  7. android:padding="10dp"
  8. android:textSize="14sp"
  9. android:textStyle="bold" />
Step 2
Create another XML file and write this:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="40dp"
  5. android:background="@android:color/black"
  6. android:clickable="true"
  7. android:orientation="vertical"
  8. android:paddingLeft="40dp"
  9. tools:context=".MainActivity" >
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="39dp"
  13. android:gravity="center_vertical" >
  14. <TextView
  15. android:id="@+id/textView1"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_marginLeft="5dp"
  19. android:textColor="#FFFFFF"
  20. android:textSize="14sp"
  21. android:textStyle="bold" />
  22. </LinearLayout>
  23. <View
  24. android:layout_width="match_parent"
  25. android:layout_height="1dp"
  26. android:background="@android:color/white" />
  27. </LinearLayout>
Step 3
Create a Java class MainActivity.java and write this:
  1. import android.app.ExpandableListActivity;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.widget.ExpandableListView;
  6. import android.widget.ExpandableListView.OnChildClickListener;
  7. import java.util.ArrayList;
  8. public class MainActivity extends ExpandableListActivity implements
  9. OnChildClickListener {
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. ExpandableListView expandbleLis = getExpandableListView();
  14. expandbleLis.setDividerHeight(2);
  15. expandbleLis.setGroupIndicator(null);
  16. expandbleLis.setClickable(true);
  17. setGroupData();
  18. setChildGroupData();
  19. NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem);
  20. mNewAdapter
  21. .setInflater(
  22. (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
  23. this);
  24. getExpandableListView().setAdapter(mNewAdapter);
  25. expandbleLis.setOnChildClickListener(this);
  26. }
  27. public void setGroupData() {
  28. groupItem.add("Android Versions");
  29. groupItem.add("Iphone versions");
  30. }
  31. ArrayList<String> groupItem = new ArrayList<String>();
  32. ArrayList<Object> childItem = new ArrayList<Object>();
  33. public void setChildGroupData() {
  34. /**
  35. * Add Data For TecthNology
  36. */
  37. ArrayList<String> child = new ArrayList<String>();
  38. child.add("Cupcake");
  39. child.add("Donut");
  40. child.add("Eclairs");
  41. child.add("Froyo");
  42. childItem.add(child);
  43. /**
  44. * Add Data For Mobile
  45. */
  46. child = new ArrayList<String>();
  47. child.add("Iphone OS 1.x");
  48. child.add("Iphone OS 2.x");
  49. child.add("Iphone OS 3.x");
  50. child.add("Iphone OS 4.x");
  51. childItem.add(child);
  52. }
  53. }
Step 4
Create a Java class and write this:
  1. import android.app.Activity;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.View.OnClickListener;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseExpandableListAdapter;
  7. import android.widget.CheckedTextView;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import java.util.ArrayList;
  11. @SuppressWarnings("unchecked")
  12. public class Adapter extends BaseExpandableListAdapter {
  13. public ArrayList<String> groupItem, tempChild;
  14. public ArrayList<Object> Childtem = new ArrayList<Object>();
  15. public LayoutInflater minflater;
  16. public Activity activity;
  17. public Adapter(ArrayList<String> grList, ArrayList<Object> childItem) {
  18. groupItem = grList;
  19. this.Childtem = childItem;
  20. }
  21. public void setInflater(LayoutInflater mInflater, Activity act) {
  22. this.minflater = mInflater;
  23. activity = act;
  24. }
  25. @Override
  26. public Object getChild(int groupPosition, int childPosition) {
  27. return null;
  28. }
  29. @Override
  30. public long getChildId(int groupPosition, int childPosition) {
  31. return 0;
  32. }
  33. @Override
  34. public View getChildView(int groupPosition, final int childPosition,
  35. boolean isLastChild, View convertView, ViewGroup parent) {
  36. tempChild = (ArrayList<String>) Childtem.get(groupPosition);
  37. TextView text = null;
  38. if (convertView == null) {
  39. convertView = minflater.inflate(R.layout.newxml, null);
  40. }
  41. text = (TextView) convertView.findViewById(R.id.textView1);
  42. text.setText(tempChild.get(childPosition));
  43. convertView.setOnClickListener(new OnClickListener() {
  44. @Override
  45. public void onClick(View v) {
  46. Toast.makeText(activity, tempChild.get(childPosition),
  47. Toast.LENGTH_SHORT).show();
  48. }
  49. });
  50. return convertView;
  51. }
  52. @Override
  53. public int getChildrenCount(int groupPosition) {
  54. return ((ArrayList<String>) Childtem.get(groupPosition)).size();
  55. }
  56. @Override
  57. public Object getGroup(int groupPosition) {
  58. return null;
  59. }
  60. @Override
  61. public int getGroupCount() {
  62. return groupItem.size();
  63. }
  64. @Override
  65. public void onGroupCollapsed(int groupPosition) {
  66. super.onGroupCollapsed(groupPosition);
  67. }
  68. @Override
  69. public void onGroupExpanded(int groupPosition) {
  70. super.onGroupExpanded(groupPosition);
  71. }
  72. @Override
  73. public long getGroupId(int groupPosition) {
  74. return 0;
  75. }
  76. @Override
  77. public View getGroupView(int groupPosition, boolean isExpanded,
  78. View convertView, ViewGroup parent) {
  79. if (convertView == null) {
  80. convertView = minflater.inflate(R.layout.activity_main, null);
  81. }
  82. ((CheckedTextView) convertView).setText(groupItem.get(groupPosition));
  83. ((CheckedTextView) convertView).setChecked(isExpanded);
  84. return convertView;
  85. }
  86. @Override
  87. public boolean hasStableIds() {
  88. return false;
  89. }
  90. @Override
  91. public boolean isChildSelectable(int groupPosition, int childPosition) {
  92. return false;
  93. }
  94. }
Step 5
Output Screen
emul1.png
When you click on Android version it gives a list like this:
emul2.png
When you click on iPhone Version:
emul8.png
When you click on any version of Android:
emul5.jpg
When you click on any version of iPhone:
emul6.jpg