Posted By Our Mentors
Amreen Khan
Android Developer
animesh bansal
iphone App Developer
Devendra chouksey
Android Developer
Dilip Saket
IOS apps developer
Hemraj Jhariya
iOS Developer
Jaihind singh Rajput
Smart Phone App Dev
Mujahid khan
Android developer
Narendra Yadav
android developer
Narinder Gupta
Mobile App Architect
Pradeep singh Thakur
Mobile App Architect
Sanjay Sharma
Andriod Developer
Shakti Rajpal
IT Architect
Soniya Vishwakarma
iPhone App Developer
Sunil Raghuwanshi
iPhone App Developer
Supriya Kapse
Senior developer
View current Topics
How to Create Multi level ExpandableListView in android? Android
Hi friends there is nothing built-in for Multilable expandable listview . But we know this is very useful for our android application development. Here we see how can we make our own Multilablexpandable list view here I just create thirdlevelexpandable list view you can expnd its how many level you want .
Posted By: Sanjay Sharma Date: 11 Jul 2012 View: 8351
      Rate This !!


Step 1. Create a project:

Project Name : vincent_threeelevellist

Package Name : com.vit

Activity Name: Vincent_threeelevellistActivity.java

Step 2. Create layout file first for our activity class.

                      main.xml

<?xml version="1.0" encoding="utf-8"?>

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 

    android:orientation="vertical"

 

     android:layout_width="fill_parent"

  

     android:layout_height="fill_parent">

 

    <ExpandableListView

   

     android:layout_width="fill_parent"

   

     android:id="@+id/ParentLevel"

   

     android:groupIndicator="@null"

   

     android:layout_height="fill_parent">

    </ExpandableListView>

 

 

 

      </LinearLayout>

Note :

      In main.xml we take a ExpandableListView in a linearlayout.

 

Step 3. Now create Your java file :-

                              Vincent_ThreeelevellistActivity.java

package vit.com;

 

import android.app.Activity;

import android.content.Context;

import android.content.res.ColorStateList;

import android.graphics.Color;

import android.os.Bundle;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseExpandableListAdapter;

import android.widget.ExpandableListView;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.LinearLayout.LayoutParams;

 

public class Vincent_ThreeelevellistActivity extends Activity {

       ExpandableListView explvlist;

 

       @Override

       public void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.main);

 

              explvlist = (ExpandableListView) findViewById(R.id.ParentLevel);

              explvlist.setAdapter(new ParentLevel());

 

       }

 

       public class ParentLevel extends BaseExpandableListAdapter {

 

              @Override

              public Object getChild(int arg0, int arg1) {

                     return arg1;

              }

 

              @Override

              public long getChildId(int groupPosition, int childPosition) {

                     return childPosition;

              }

 

              @Override

              public View getChildView(int groupPosition, int childPosition,

                           boolean isLastChild, View convertView, ViewGroup parent) {

                     CustExpListview SecondLevelexplv = new CustExpListview(Vincent_ThreeelevellistActivity.this);

                     SecondLevelexplv.setAdapter(new SecondLevelAdapter());

                     SecondLevelexplv.setGroupIndicator(null);

                     return SecondLevelexplv;

              }

 

              @Override

              public int getChildrenCount(int groupPosition) {

                     return 3;

              }

 

              @Override

              public Object getGroup(int groupPosition) {

                     return groupPosition;

              }

 

              @Override

              public int getGroupCount() {

                     return 5;

              }

 

              @Override

              public long getGroupId(int groupPosition) {

                     return groupPosition;

              }

 

              @Override

              public View getGroupView(int groupPosition, boolean isExpanded,

                           View convertView, ViewGroup parent) {

                     TextView tv = new TextView(Vincent_ThreeelevellistActivity.this);

                     tv.setText("->FirstLevel");

                     tv.setTextColor(Color.BLACK);

                     tv.setTextSize(20);

                     tv.setBackgroundColor(Color.BLUE);

                     tv.setPadding(10, 7, 7, 7);

 

                     return tv;

              }

 

              @Override

              public boolean hasStableIds() {

                     return true;

              }

 

              @Override

              public boolean isChildSelectable(int groupPosition, int childPosition) {

                     return true;

              }

       }

 

       public class CustExpListview extends ExpandableListView {

 

              int intGroupPosition, intChildPosition, intGroupid;

 

              public CustExpListview(Context context) {

                     super(context);

              }

 

              protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

                     widthMeasureSpec = MeasureSpec.makeMeasureSpec(960,

                                  MeasureSpec.AT_MOST);

                     heightMeasureSpec = MeasureSpec.makeMeasureSpec(600,

                                  MeasureSpec.AT_MOST);

                     super.onMeasure(widthMeasureSpec, heightMeasureSpec);

              }

       }

 

       public class SecondLevelAdapter extends BaseExpandableListAdapter {

 

              @Override

              public Object getChild(int groupPosition, int childPosition) {

                     return childPosition;

              }

 

              @Override

              public long getChildId(int groupPosition, int childPosition) {

                     return childPosition;

              }

 

              @Override

              public View getChildView(int groupPosition, int childPosition,

                           boolean isLastChild, View convertView, ViewGroup parent) {

                     TextView tv = new TextView(Vincent_ThreeelevellistActivity.this);

                     tv.setText("child");

                     tv.setTextColor(Color.BLACK);

                     tv.setTextSize(20);

                     tv.setPadding(15, 5, 5, 5);

                     tv.setBackgroundColor(Color.YELLOW);

                     tv.setLayoutParams(new ListView.LayoutParams(

                                  LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

                     return tv;

              }

 

              @Override

              public int getChildrenCount(int groupPosition) {

                     return 5;

              }

 

              @Override

              public Object getGroup(int groupPosition) {

                     return groupPosition;

              }

 

              @Override

              public int getGroupCount() {

                     return 1;

              }

 

              @Override

              public long getGroupId(int groupPosition) {

                     return groupPosition;

              }

 

              @Override

              public View getGroupView(int groupPosition, boolean isExpanded,

                           View convertView, ViewGroup parent) {

                     TextView tv = new TextView(Vincent_ThreeelevellistActivity.this);

                     tv.setText("-->Second Level");

                     tv.setTextColor(Color.BLACK);

                     tv.setTextSize(20);

                     tv.setPadding(12, 7, 7, 7);

                     tv.setBackgroundColor(Color.RED);

 

                     return tv;

              }

 

              @Override

              public boolean hasStableIds() {

                     // TODO Auto-generated method stub

                     return true;

              }

 

              @Override

              public boolean isChildSelectable(int groupPosition, int childPosition) {

                     // TODO Auto-generated method stub

                     return true;

              }

 

       }

}

Note :

      I m explain whole  above code here in our Vincent_ThreeelevellistActivity.java we map expandablelistview with its id and set a adapter which is our custom adapter class. ParentLevel is adapter class which is extend BaseExpandableListAdapter and in its getChildView method  we also set adapter for a second level listview which is a custom class.

Step 4: When you run application you will get following screen as a output:


When you click first level you will get following screen as a output:


On clicking second level you will get following output:

That’s it

Full source code can be downloaded from the provided link

Any queries and suggestions are welcome

 

Regards

Sanjay Sharma

Vincent IT Inc


Posted Queries
Posted By: mehul Bhundiya Reply:How to Create Multi level ExpandableListVie
02 Jan 2014
i can't find Download link for miltilevel expandable listview in android so please give me the miltilevel expandable listview download Link
Posted By: mehul Bhundiya Reply:How to Create Multi level ExpandableListVie
02 Jan 2014
i can't find Download link for miltilevel expandable listview in android so please give me the miltilevel expandable listview download Link
Posted By: Sudheesh Mohan Reply:How to Create Multi level ExpandableListVie
08 Dec 2014
Couldnt find the link for source code.....
Posted By: Sudheesh Mohan Reply:How to Create Multi level ExpandableListVie
08 Dec 2014
Couldnt find the link for source code.....
Posted By: Ashish Kumar Reply:How to Create Multi level ExpandableListVie
21 Oct 2014
Hello every body! Actually i want to make multilevel ExpandableListView but every group have different-different number of childs. So, any one have any idea? Please post your idea.
Posted By: vj vj Reply:How to Create Multi level ExpandableListVie
27 Feb 2013
getting errors when extracting the Zip files.. having the problem for all the zip files..
Posted By: rani kapur Reply:Reply:How to Create Multi level ExpandableL
17 Apr 2013
hi thanks for source code . The program was successfully implemented . Please advise how I can get childPosition value I've used the following code: explvlist.setOnChildClickListener(new OnChildClickListener() { public boolean onChildClick(ExpandableListView parent,View v, int groupPosition, int childPosition, long id) { tv.setText(String.valueOf("childPosition:" + childPosition)); return false; } }); but it But it does not work ! please help me (: