Android ExpandableListView Example Tutorial

Create ExpandableListView in android using ArrayList<String>+ BaseExpandableListAdapter class.

ExpandableListView is type of drop down list view in android apps this listview gives even more flexibility to app user. On ExpandableListView  there are two type of elements present first is parent list item and second is child element item. Parent item is created using List<String> and child sub element is created using String[] . So here is the complete step by step tutorial for Android ExpandableListView Example Tutorial.

android-project-download-code-button

Android ExpandableListView Example Tutorial.

Code for MainActivity.java file.

 package com.android_examples.com.expandablelistview;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 List<String> ChildList;
 Map<String, List<String>> ParentListItems;
 ExpandableListView expandablelistView;
 
 // Assign Parent list items here.
 List<String> ParentList = new ArrayList<String>();
 { ParentList.add("ANDROID");
 ParentList.add("PHP");
 ParentList.add("BLOGGER");
 ParentList.add("PHOTOSHOP");
 ParentList.add("WORDPRESS");
 ParentList.add("SEO"); }
 
 // Assign children list element using string array.
 String[] AndroidName = { "ANDROID STUDIO","ANDROID EXAMPLES","ANDROID TUTORIALS" };
 String[] PhpName = { "XAMPP","PHPMYADMIN","MYSQL" };
 String[] BloggerName = { "BLOG NAME","SUB DOMAIN NAME","BLOGGER" };
 String[] PhotoshopName = { "COLOR TOOL","MOVE TOOL","CROP TOOL" };
 String[] WordPressName = { "LOGIN","ADD NEW POST","PLUGINS" };
 String[] SEOName = { "SEARCH","ENGINE","OPTIMIZATION" };
 String[] ByDefalutMessage = {"Items Loading"};
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 ParentListItems = new LinkedHashMap<String, List<String>>();

 for ( String HoldItem : ParentList) {
 if (HoldItem.equals("ANDROID")) {
 loadChild(AndroidName);
 } else if (HoldItem.equals("PHP"))
 loadChild(PhpName);
 else if (HoldItem.equals("BLOGGER"))
 loadChild(BloggerName);
 else if (HoldItem.equals("PHOTOSHOP"))
 loadChild(PhotoshopName);
 else if (HoldItem.equals("WORDPRESS"))
 loadChild(WordPressName);
 else if (HoldItem.equals("SEO"))
 loadChild(SEOName);
 else
 loadChild(ByDefalutMessage);

 ParentListItems.put(HoldItem, ChildList);
}
 
 expandablelistView = (ExpandableListView) findViewById(R.id.expandableListView1);
 final ExpandableListAdapter expListAdapter = new ListAdapter(
 this, ParentList, ParentListItems);
 expandablelistView.setAdapter(expListAdapter);
 
 expandablelistView.setOnChildClickListener(new OnChildClickListener() {
 
 @Override
 public boolean onChildClick(ExpandableListView parent, View v,
 int groupPosition, int childPosition, long id) {
 // TODO Auto-generated method stub
 
 final String selected = (String) expListAdapter.getChild(
 groupPosition, childPosition);
 Toast.makeText(getBaseContext(), selected, Toast.LENGTH_LONG)
 .show();
 
 return true;
 }
 });
 }
 
 private void loadChild(String[] ParentElementsName) {
 ChildList = new ArrayList<String>();
 for (String model : ParentElementsName)
 ChildList.add(model);
 }

}

Code for ListAdapter.java  file.

 package com.android_examples.com.expandablelistview;

import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
 
public class ListAdapter extends BaseExpandableListAdapter {
 
 private Activity context;
 private Map<String, List<String>> ParentListItems;
 private List<String> Items;
 
 public ListAdapter(Activity context, List<String> Items,
 Map<String, List<String>> ParentListItems) {
 this.context = context;
 this.ParentListItems = ParentListItems;
 this.Items = Items;
 }
 
 public Object getChild(int groupPosition, int childPosition) {
 return ParentListItems.get(Items.get(groupPosition)).get(childPosition);
 }
 
 public long getChildId(int groupPosition, int childPosition) {
 return childPosition;
 }
 
 
 public View getChildView(final int groupPosition, final int childPosition,
 boolean isLastChild, View ListView, ViewGroup parent) {
 final String CoursesName = (String) getChild(groupPosition, childPosition);
 LayoutInflater inflater = context.getLayoutInflater();
 
 if (ListView == null) {
 ListView = inflater.inflate(R.layout.child_list_item, null);
 }
 
 TextView item = (TextView) ListView.findViewById(R.id.textView1);
 
 item.setText(CoursesName);
 return ListView;
 }
 
 public int getChildrenCount(int groupPosition) {
 return ParentListItems.get(Items.get(groupPosition)).size();
 }
 
 public Object getGroup(int groupPosition) {
 return Items.get(groupPosition);
 }
 
 public int getGroupCount() {
 return Items.size();
 }
 
 public long getGroupId(int groupPosition) {
 return groupPosition;
 }
 
 public View getGroupView(int groupPosition, boolean isExpanded,
 View ListView, ViewGroup parent) {
 String CoursesFull = (String) getGroup(groupPosition);
 if (ListView == null) {
 LayoutInflater infalInflater = (LayoutInflater) context
 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 ListView = infalInflater.inflate(R.layout.parent_list_item,null);
 }
 TextView item = (TextView) ListView.findViewById(R.id.textView1);
 item.setText(CoursesFull);
 return ListView;
 }
 
 public boolean hasStableIds() {
 return true;
 }
 
 public boolean isChildSelectable(int groupPosition, int childPosition) {
 return true;
 }
}

Code for activity_main.xml file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.expandablelistview.MainActivity" >

 <ExpandableListView
 android:id="@+id/expandableListView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" >
 </ExpandableListView>

</RelativeLayout>

Code for parent_list_item.xml layout file.

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

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Medium Text"
 android:textAppearance="?android:attr/textAppearanceMedium"
 android:padding="30dp" />

</LinearLayout>

Code for child_list_item.xml layout file.

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:padding="40dp" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Medium Text"
 android:textAppearance="?android:attr/textAppearanceMedium"
 />

</LinearLayout>

Screenshots:

Android ExpandableListView Example Tutorial

ExpandableListView

Click Here To Download Android ExpandableListView Example Tutorial project.

Leave a Reply

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