Saturday, 22 March 2014

Static ExpandableListView Creation in Android App and Implementation

This blog describe about ExpandableListView creation in android apps. Through this can create more than one group including with multiple child data with each group with expand and hide list data feature.

main.xml

<LinearLayout 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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Expandable List View"
        android:layout_margin="20dp"
        android:textColor="#336633"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_gravity="center"/>

    <ExpandableListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
    </ExpandableListView>

</LinearLayout>


ExpandableListViewActivity.java

package com.rakesh.tiwari.expandablelistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;

public class ExpandableListViewActivity extends ExpandableListActivity {

    private     ExpandableListAdapter listAdptr;
    ExpandableListView expLv;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

           List<Map<String,String>> parentGroup= new ArrayList<Map<String, String>>();
    List<List<Map<String,String>>> childGroup=new ArrayList<List<Map<String,String>>>();
   
    // Start group1 with childs....................................................................
   
    Map<String,String> parentGroup1=new HashMap<String,String>();
    parentGroup.add(parentGroup1);
    parentGroup1.put("parent", "Country");
   
    //  Child data 1..............................................................................
    List<Map<String,String>> childGroup1=new ArrayList<Map<String,String>>();
    Map<String,String> childData1=new HashMap<String,String>();
    childGroup1.add(childData1);
    childData1.put("child","India");
   
//  Child data 2..............................................................................
   
    Map<String,String> childData2=new HashMap<String,String>();
    childGroup1.add(childData2);
    childData2.put("child","Japan");
   
//  Child data 3..............................................................................
   
    Map<String,String> childData3=new HashMap<String,String>();
    childGroup1.add(childData3);
    childData3.put("child","Rusia");
   
//  Child data 4..............................................................................
   
    Map<String,String> childData4=new HashMap<String,String>();
    childGroup1.add(childData4);
    childData4.put("child","USA");
   
//  Child data 5..............................................................................
   
    Map<String,String> childData5=new HashMap<String,String>();
    childGroup1.add(childData5);
    childData5.put("child","France");
    childGroup.add(childGroup1);

    // End of group1 with childs....................................................................
   
    // Start group2 with childs....................................................................
   
       
        Map<String,String> parentGroup2=new HashMap<String,String>();
        parentGroup.add(parentGroup2);
        parentGroup2.put("parent", "Capital");
       
        //  Child data 1..............................................................................
        List<Map<String,String>> childGroup2=new ArrayList<Map<String,String>>();
        Map<String,String> childData21=new HashMap<String,String>();
        childGroup2.add(childData21);
        childData21.put("child","Delhi");
       
    //  Child data 2..............................................................................
       
        Map<String,String> childData22=new HashMap<String,String>();
        childGroup2.add(childData22);
        childData22.put("child","Tokyo");
       
    //  Child data 3..............................................................................
       
        Map<String,String> childData23=new HashMap<String,String>();
        childGroup2.add(childData23);
        childData23.put("child","Moscow");
       
    //  Child data 4..............................................................................
       
        Map<String,String> childData24=new HashMap<String,String>();
        childGroup2.add(childData24);
        childData24.put("child","New York");
       
    //  Child data 5..............................................................................
       
        Map<String,String> childData25=new HashMap<String,String>();
        childGroup2.add(childData25);
        childData25.put("child","Paris");
       
        childGroup.add(childGroup2);
       
        listAdptr=new SimpleExpandableListAdapter(this, parentGroup,android.R.layout.simple_expandable_list_item_1,
                new String[]{"parent"}, new int[]{android.R.id.text1,android.R.id.text2},
                childGroup,android.R.layout.simple_expandable_list_item_2,new String[]{"child"},
                new int[]{android.R.id.text1});
           
        setListAdapter(listAdptr);
        expLv=getExpandableListView();

        expLv.setOnChildClickListener(new OnChildClickListener() {
           
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
               
                switch(groupPosition){
                case 0:
                    switch(childPosition){
                    case 0:
                        Toast.makeText(getApplicationContext(), "India", Toast.LENGTH_LONG).show();
                        break;   
                    case 1:   
                        Toast.makeText(getApplicationContext(), "Japan", Toast.LENGTH_LONG).show();
                    break;
                    case 2:
                    Toast.makeText(getApplicationContext(), "Rusia", Toast.LENGTH_LONG).show();
                    break;
                    case 3:
                        Toast.makeText(getApplicationContext(), "USA",Toast.LENGTH_LONG).show();
                    break;
                   
                    case 4:
                        Toast.makeText(getApplicationContext(), "France", Toast.LENGTH_LONG).show();
                        break;
                   
                    }
                case 1:
                    switch(childPosition){
                    case 0:
                        Toast.makeText(getApplicationContext(), "Delhi", Toast.LENGTH_LONG).show();
                        break;
                    case 1:
                        Toast.makeText(getApplicationContext(), "Tokyo", Toast.LENGTH_LONG).show();
                        break;
                    case 2:
                        Toast.makeText(getApplicationContext(), "Moscow", Toast.LENGTH_LONG).show();
                        break;
                    case 3:
                        Toast.makeText(getApplicationContext(), "New York", Toast.LENGTH_LONG).show();
                        break;
                    case 4:
                        Toast.makeText(getApplicationContext(), "Paris", Toast.LENGTH_LONG).show();
                        break;
                   
                    }
                }
               
                return false;
            }
        });
        }

   
   
    // End of group1 with childs....................................................................
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.expandable_list_view, menu);
        return true;
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakesh.tiwari.expandablelistview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rakesh.tiwari.expandablelistview.ExpandableListViewActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>





ExpandableListView Android APP
ExpandableListView Android APP

ExpandableListView Android APP
ExpandableListView Android APP

Hide ListView Data
Hide ListView Data

Show ListView Data
Show ListView Data

Hide / Show ListView Data
Hide / Show ListView Data