expandable list android

Expandable ListView in android example

Expandable ListView in android example

ExpandableListView is a two-level ListView that scroll vertically. This view show it’s child view by expanding the groups view and also collapsed the child by clicking on the group view in list.

We already learn in our previous tutorial how to create a ListView in android. Now in this tutorial we are going to learn how expandable list view works, it data collection and expandable adapter. We also learn how expand and collapse groups in ExpandableListView in android.

Here we use one Activity class ‘ExpandableListActivity.java’, one data collection class ‘ExpandableCollection.java’ and one adapter class ‘AndroidExpandableListAdapter.java’.

Now let’s check the source code:-

ExpandableListActivity.java


 

package com.androidfromhome.expandablelist;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class ExpandableListActivity extends Activity {

    private ExpandableListView elv_android;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable_list);
        
        
        elv_android=(ExpandableListView)findViewById(R.id.elv_android);
        elv_android.setGroupIndicator(null);
        
        ExpandableCollection.key_value=new ArrayList<String>();
        ExpandableCollection.expandable_main_arr=new ArrayList<ExpandableCollection>();
        ExpandableCollection.expandable_hashmap=new HashMap<String,ArrayList<ExpandableCollection>>();
        
                
        ExpandableCollection.key_value.add("Insect");
        ExpandableCollection.key_value.add("Natural");
        ExpandableCollection.key_value.add("Environment");
        
        
        ArrayList<ExpandableCollection> arr_obj1=new ArrayList<ExpandableCollection>();
        
        arr_obj1.add(new ExpandableCollection("Butterfly", "Butterflies are " +
                "part of the class of Insects in the order Lepidoptera", R.drawable.butterfly));
        ExpandableCollection.expandable_hashmap.put(ExpandableCollection.key_value.get(0), arr_obj1);
        
        
        ArrayList<ExpandableCollection> arr_obj2=new ArrayList<ExpandableCollection>();
        
        arr_obj2.add(new ExpandableCollection("Green Scenery", "Green Scenery " +
                "is good for health", R.drawable.green_scenery));
        arr_obj2.add(new ExpandableCollection("Leaf", "A leaf is an organ " +
                "of a vascular plant and is the principal lateral appendage of the stem.", R.drawable.leaf));
        arr_obj2.add(new ExpandableCollection("One Rose", "A rose is a woody perennial " +
                "of the genus Rosa, within the family Rosaceae", R.drawable.one_rose));
        ExpandableCollection.expandable_hashmap.put(ExpandableCollection.key_value.get(1), arr_obj2);
        
        
        ArrayList<ExpandableCollection> arr_obj3=new ArrayList<ExpandableCollection>();
        
        arr_obj3.add(new ExpandableCollection("Open Sky", "The sky (or celestial dome) is everything that lies above the surface of " +
                "the Earth, including the atmosphere and outer space.", R.drawable.sky));
        
        ExpandableCollection.expandable_hashmap.put(ExpandableCollection.key_value.get(2), arr_obj3);
        
        
        
        
        AndroidExpandableListAdapter adapter=new AndroidExpandableListAdapter(ExpandableListActivity.this, ExpandableListActivity.this,
                ExpandableCollection.key_value,
                ExpandableCollection.expandable_hashmap);
        
        elv_android.setAdapter(adapter);
        
        elv_android.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                
                    if (ExpandableCollection.key_value!=null) {
                    if (ExpandableCollection.key_value.size()>groupPosition) {
                        String key=ExpandableCollection.key_value.get(groupPosition);
                    
                        if (ExpandableCollection.expandable_hashmap.size()>0) {
                            ExpandableCollection obj_exp    =ExpandableCollection.expandable_hashmap
                            .get(key).get(childPosition);
                            
                            Toast.makeText(ExpandableListActivity.this, "Details :"+obj_exp.message,
                                    Toast.LENGTH_LONG).show();
                        }}}
                    return false;}
        });
    }

    
}

 


Data collection class

ExpandableCollection.java


package com.androidfromhome.expandablelist;

import java.util.ArrayList;
import java.util.HashMap;

public class ExpandableCollection {
    
    public String title="";
    public String message="";
    public int image=0;
    
    
    public static HashMap<String, ArrayList<ExpandableCollection>> expandable_hashmap;
    public static ArrayList<ExpandableCollection> expandable_main_arr=null;
    public static ArrayList<String> key_value=null;
    
    
public ExpandableCollection(String title,String message,int image){
    
    this.title=title;
    this.message=message;
    this.image=image;
    }
}


AndroidExpandableListAdapter.java


package com.androidfromhome.expandablelist;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AndroidExpandableListAdapter extends BaseExpandableListAdapter {

    Context ctx;
    Activity act;
    ArrayList<String> headerCollectionArr;
    HashMap<String, ArrayList<ExpandableCollection>> holderHash;
    
    
    public AndroidExpandableListAdapter(Context ctx,Activity act,
            ArrayList<String> headerCollectionArr,
            HashMap<String, ArrayList<ExpandableCollection>> holderHash) {
        this.ctx = ctx;
        this.headerCollectionArr = headerCollectionArr;
        this.holderHash = holderHash;
        this.act=act;
    }
    

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        String st=headerCollectionArr.get(groupPosition);
    
        Object obj=null;
        if (holderHash.size()>0) {
            obj    =holderHash.get(st).get(childPosititon);
        }
        
        return obj;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        ExpandableCollection exp_obj =  (ExpandableCollection) getChild(groupPosition, childPosition);
        
        ChildViewHolder viewHolder=new ChildViewHolder();
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) ctx
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.class_booking_item, null);
            viewHolder.tv_title= (TextView) convertView
                    .findViewById(R.id.tv_inner_list);
            
            viewHolder.iv_image=(ImageView) convertView
                    .findViewById(R.id.iv_image);
            
                convertView.setTag(viewHolder);
            
            
        }else
        {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }
        
        
        viewHolder.tv_title.setText(exp_obj.title);    
        viewHolder.iv_image.setImageResource(exp_obj.image);
        
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        String st=headerCollectionArr.get(groupPosition);
        int i = 0;
        if (holderHash.size()>0) {
            ArrayList<ExpandableCollection> ll=holderHash.get(st);
        if (ll!=null) {
            i=ll.size();    
        }else{
            
            i=0;
        }
            
        }
        return i;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return headerCollectionArr.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return headerCollectionArr.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String)getGroup(groupPosition);
        
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater)ctx
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.expended_header, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.tv_header);
        ImageView image = (ImageView) convertView.findViewById(R.id.iv_exp);
        
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        
        
            int imageResourceId = isExpanded ? R.drawable.seconddown : R.drawable.arrow_list;
            image.setImageResource(imageResourceId);
            
            image.setVisibility(View.VISIBLE);
        
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    
    public class ChildViewHolder {

        TextView tv_title;
        ImageView iv_image;
      
    }
}


xml source code

activity_expandable_list.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <ExpandableListView
        android:id="@+id/elv_android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>

</LinearLayout>


class_booking_item.xml


<?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:background="#343434"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <FrameLayout
            android:layout_width="60dp"
            android:layout_height="70dp"
            android:layout_margin="4dp"
            android:padding="6dp" >

            <ImageView
                android:id="@+id/iv_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:padding="8dp" />
        </FrameLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_inner_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:padding="5dp"
                android:text="Pajama Party"
                android:textColor="#ffffff"
                android:textSize="16dp" />

        </LinearLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="2dp"
            android:padding="6dp" >

            <ImageView
                android:id="@+id/iv_child"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center"
                android:background="@drawable/bluewithplus"
                android:padding="8dp" />

        </FrameLayout>

    </LinearLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000" />

</LinearLayout>


expended_header.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#dfdfdf"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="5dp" >

    <TextView
        android:id="@+id/tv_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView"
        android:textColor="#000000"
        android:textSize="16dp"
        android:textStyle="bold" />

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:padding="6dp" >

        <ImageView
            android:id="@+id/iv_exp"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_margin="4dp"
            android:background="@drawable/arrow_list" />

    </FrameLayout>

</LinearLayout>


Download Full Source Code

Related Post:-

Simple ListView in android example with custom layout

Internet connection checking in android device

Switching One activity  to another activity in Android by intent

How to get the call log details in android

Leave a Reply

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

Get Widget