About Piyas De

Piyas is Sun Microsystems certified Enterprise Architect with 10+ years of professional IT experience in various areas such as Architecture Definition, Define Enterprise Application, Client-server/e-business solutions.

Expandable List in Android

Today we are going to discuss ExpandableListView in Android. Here I’ve used Eclipse with ADT plugin to build this example. The example is straightforward. Create a new Android Application Project in your workspace. Extend your main Activity from ExpandableListActivity.

For your reference please find the following code :
 
 
 
 
 

package com.example.expandablelistviewdemo;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class MainActivity extends ExpandableListActivity {
private static final String arrGroupElements[] = { "Item1", "Item2", "Item3",
"Item4", "Item5", "Item6", "Item7", "Item8", "Item9" };
/**
* strings for child elements
*/
private static final String arrChildElements[][] = {
{ "Details1 A","Details1 B", "Details1 C" },
{ "Details2 A","Details2 B", "Details2 C" },
{ "Details3 A","Details3 B", "Details3 C" },
{ "Details4 A","Details4 B", "Details4 C" },
{ "Details5 A","Details5 B", "Details5 C" },
{ "Details6 A","Details6 B", "Details6 C" },
{ "Details7" },
{ "Details8" },
{ "Details9" }
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.article_screen);
setListAdapter(new ExpandableListAdapter(this));
}
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context myContext;
public ExpandableListAdapter(Context context) {
myContext = context;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(
R.layout.article_list_child_item_layout, null);
}
TextView yourSelection = (TextView) convertView
.findViewById(R.id.articleContentTextView);
yourSelection
.setText(arrChildElements[groupPosition][childPosition]);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return arrChildElements[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public int getGroupCount() {
return arrGroupElements.length;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(
R.layout.article_list_item_layout, null);
}
TextView groupName = (TextView) convertView
.findViewById(R.id.articleHeaderTextView);
groupName.setText(arrGroupElements[groupPosition]);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}

Now we need to create the layout files. Here we’ll create 3 layout files. The first one for the main layout, the second one for the expandable list item and the third one for the expandable list sub item.

Please find the following codes :

Main Layout file (article_screen.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView android:id="@+id/articleListHeaderTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="List Header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0585C4"
android:textSize="14dp"
android:textStyle="bold" />
<ExpandableListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/articleListHeaderTextView"
android:layout_margin="10dp"
android:divider="@android:color/darker_gray"
android:childDivider="@android:color/white"
android:dividerHeight="2dp"
android:fadingEdge="none">
</ExpandableListView>
</RelativeLayout>

Expandable list item layout file (article_list_item_layout.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:paddingBottom="10dp"
android:paddingLeft="25dp"
android:paddingTop="10dp">
<TextView android:id="@+id/articleHeaderTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="false"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0585C4"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>

Expandable list item child layout file(article_list_child_item_layout.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFE4DEDE"
android:padding="10dp">
<TextView android:id="@+id/articleContentTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#FF000000" />
</RelativeLayout>

That’s it. Now you can run this application either on emulator or on real device. It should look like the following images :

Android ExpandableListView example (Expanded)

Android ExpandableListView example (Expanded)

Android ExpandableListView

Android ExpandableListView


 

Reference: Expandable List in Android from our JCG partner Piyas De at the Phlox Blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Expandable List in Android"

  1. Wallace Palmer says:

    I followed your tutorial which works perfectly, so thank you. I am trying to pass the child item to a new activity once it has been clicked. However, I cannot figure out how to do this. my intent works, I just dont know what data to pass. can you help me?

    • Samik Bandyopadhyay says:

      Here I’ve written this example using simple String elements which can be passed simply via Intent to other activity using Intent.putExtra(String key, String value). In case you have any other custom object please implement Parcelable interface and send it the same way via intent.

Leave a Reply


7 + three =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close