Android ExpandableListView
Android ExpandableListView
- Static ExpandableListView Creation in Android
- Dynamic ExpandableListView Creation in Android
- Remove List Items in Android
Static ExpandableListView Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.
Step 2 : Open res -> layout ->
activity_main.xml (or) main.xml and add following code :
<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" >
<ExpandableListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
</ExpandableListView>
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
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.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;
public class MainActivity extends ExpandableListActivity {
private ExpandableListAdapter mAdapter;
ExpandableListView expand;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
/* ******************** Start Group ********************* */
Map<String, String> curgroupMap1 = new HashMap<String, String>();
groupData.add(curgroupMap1);
curgroupMap1.put("parent", "H1");
List<Map<String,String>> children1 =new ArrayList<Map<String,String>>();
/* *** ChildData ***/
Map<String, String> curChildMap1 = new HashMap<String, String>();
children1.add(curChildMap1);
curChildMap1.put("child", "S1");
/* *** ChildData ***/
Map<String, String> curChildMap2 = new HashMap<String, String>();
children1.add(curChildMap2);
curChildMap2.put("child", "S2");
/* *** ChildData ***/
Map<String, String> curChildMap3 = new HashMap<String, String>();
children1.add(curChildMap3);
curChildMap3.put("child", "S3");
/* ***ChildData ***/
Map<String, String> curChildMap4 = new HashMap<String, String>();
children1.add(curChildMap4);
curChildMap4.put("child", "S4");
/* ***ChildData ***/
Map<String, String> curChildMap5 = new HashMap<String, String>();
children1.add(curChildMap5);
curChildMap5.put("child", "S5");
childData.add(children1);
/* *************************End Group **************************/
/* ******************** Start Group ********************* */
Map<String, String> curgroupMap2 = new HashMap<String, String>();
groupData.add(curgroupMap2);
curgroupMap2.put("parent", "H2");
List<Map<String,String>> children2 =new ArrayList<Map<String,String>>();
/* *** ChildData ***/
Map<String, String> curChildMap6 = new HashMap<String, String>();
children2.add(curChildMap6);
curChildMap6.put("child", "G1");
/* *** ChildData ***/
Map<String, String> curChildMap7 = new HashMap<String, String>();
children2.add(curChildMap7);
curChildMap7.put("child", "G2");
/* *** ChildData ***/
Map<String, String> curChildMap8 = new HashMap<String, String>();
children2.add(curChildMap8);
curChildMap8.put("child", "G3");
childData.add(children2);
/* *************************End Group **************************/
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { "parent" },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] {"child"},
new int[] { android.R.id.text1 }
);
setListAdapter(mAdapter);
expand = getExpandableListView();
expand.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(getBaseContext(), "S1",
Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getBaseContext(), "S2",
Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getBaseContext(), "S3",
Toast.LENGTH_SHORT).show();
break;
case 3 :
Toast.makeText(getBaseContext(), "S4",
Toast.LENGTH_SHORT).show();
break;
case 4 :
Toast.makeText(getBaseContext(), "S5",
Toast.LENGTH_SHORT).show();
break;
}
break;
case 1:
switch(childPosition)
{
case 0 :
Toast.makeText(getBaseContext(), "G1",
Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getBaseContext(), "G2",
Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getBaseContext(), "G3",
Toast.LENGTH_SHORT).show();
break;
}
break;
}
return false;
}
});
}
}
Step 4 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
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>
Step 5 : Our output will be like this :
Dynamic ExpandableListView Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.