STEP 1:- Create the New Android Project In your Android Eclipse tools.
< / RelativeLayout>
STEP 3:- Create list_group.xml & list_childitem.xml file in /layout folder.
:- we create these xml file for displaying our Expandable content.
:- put following code in appropriate .xml fil.
STEP 4:- Edit list_group.xml
STEP 5:- Edit list_childitem.xml
STEP 6:- Open MainActivity.java file and Edit.
STEP:- 7 Now Create the New class in your package structure.
:-Ex CustomExpandAdpter.java And Extend from the BaseExpandableListAdapter
see the following code.
STEP :- 8 Now Run this application in your Android Emulator .
- Ex:-Expandableviewapp (My application Name)
- Now You can see the Application Structure.
< 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
< ExpandableListView
android:id="@+id/exp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
< / RelativeLayout>
STEP 3:- Create list_group.xml & list_childitem.xml file in /layout folder.
:- we create these xml file for displaying our Expandable content.
:- put following code in appropriate .xml fil.
STEP 4:- Edit list_group.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:orientation="vertical"
android:background="@android:color/black"
>
< TextView
android:id="@+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:paddingLeft="25dp"
android:textColor="#f9f93d" />
< / LinearLayout>
STEP 5:- Edit list_childitem.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:orientation="vertical" >
< TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
/>
< / LinearLayout>
STEP 6:- Open MainActivity.java file and Edit.
package com.example.expandableviewapp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
public class MainActivity extends Activity {
ExpandableListView exListView;
ExpandableListAdapter eListAdapter;
List parent;
HashMap> child;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exListView=(ExpandableListView)findViewById(R.id.exp);
preparelistdata(); // populate the data for arrray and
//int[] res=new int[R.layout.list_group,R.layout.list_childitem]
eListAdapter=new CustomExpandAdpter(this,parent,child);
exListView.setAdapter(eListAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void preparelistdata()
{
//data population for expandableview parent=new ArrayList();
parent.add("Jnext");
parent.add("Java");
parent.add("Android");
child =new HashMap>();
List Jnext=new ArrayList();
Jnext.add("software Development");
Jnext.add("Training");
Jnext.add("Resume Buillding");
Jnext.add("Testing");
Jnext.add("Interview pretion");
List java=new ArrayList();
java.add("object");
java.add("core java");
java.add("exception");
java.add("collection");
List android=new ArrayList();
android.add("introdution");
android.add("layout");
android.add("xml");
android.add("web services");
//String[] a= new String[]{parent.get(0),parent.get(1),parent.get(2)};
child.put(parent.get(0), Jnext);
child.put(parent.get(1), java);
child.put(parent.get(2), android);
// return a;
} }
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
public class MainActivity extends Activity {
ExpandableListView exListView;
ExpandableListAdapter eListAdapter;
List
HashMap
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exListView=(ExpandableListView)findViewById(R.id.exp);
preparelistdata(); // populate the data for arrray and
//int[] res=new int[R.layout.list_group,R.layout.list_childitem]
eListAdapter=new CustomExpandAdpter(this,parent,child);
exListView.setAdapter(eListAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void preparelistdata()
{
//data population for expandableview parent=new ArrayList
parent.add("Jnext");
parent.add("Java");
parent.add("Android");
child =new HashMap
List
Jnext.add("software Development");
Jnext.add("Training");
Jnext.add("Resume Buillding");
Jnext.add("Testing");
Jnext.add("Interview pretion");
List
java.add("object");
java.add("core java");
java.add("exception");
java.add("collection");
List
android.add("introdution");
android.add("layout");
android.add("xml");
android.add("web services");
//String[] a= new String[]{parent.get(0),parent.get(1),parent.get(2)};
child.put(parent.get(0), Jnext);
child.put(parent.get(1), java);
child.put(parent.get(2), android);
// return a;
} }
STEP:- 7 Now Create the New class in your package structure.
:-Ex CustomExpandAdpter.java And Extend from the BaseExpandableListAdapter
see the following code.
package com.example.expandableviewapp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.R.layout;
import android.R.string;
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.TextView;
public class CustomExpandAdpter extends BaseExpandableListAdapter
{
Context context;
List< String> lst;
HashMap< String, List< String >> hm;
public CustomExpandAdpter(Context context, List lst, HashMap> hm)< br/>
{
this.context=context;
this.lst=lst;
this.hm=hm;
}
// Gets the number of children in a specified group. @Override
public int getGroupCount()
{
System.out.println("======= lst size***** :======="+lst.size());
return lst.size();
}
//Gets the data associated with the given group.
@Override
public int getChildrenCount(int groupPosition) {
System.out.println("=====group position====="+groupPosition);
System.out.println("========lst get======");
System.out.println("============child sizeeeeee=========="+hm.get(lst.get(groupPosition)).size());
return hm.get(lst.get(groupPosition)).size();
}
//Gets the data associated with the given child within the given group.
@Override
public Object getGroup(int groupPosition) {
return lst.get(groupPosition);
}
//Gets the data associated with the given child within the given group.
@Override
public Object getChild(int groupPosition, int childPosition)
{
System.out.println("======getchild========"+hm.get(lst.get(groupPosition)).get(childPosition));
return hm.get(lst.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
} //this method is use for view parent that you want to expand.
@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
String grouptext=(String)getGroup(groupPosition);
if (convertView==null) {
LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=layoutInflater.inflate(R.layout.list_group, null);
} TextView t=(TextView)convertView.findViewById(R.id.lblListHeader);
t.setTypeface(null, Typeface.BOLD);
t.setText(grouptext);
return convertView;
}
@Override
// This method is use for dispayling group of child for apporopriate parent.
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
String childtxt=(String)getChild(groupPosition, childPosition);
System.out.println("=============child text is:="+childtxt);
if (convertView==null) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.list_childitem,null);
}
TextView tw=(TextView)convertView.findViewById(R.id.lblListItem);
tw.setText(childtxt);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.R.layout;
import android.R.string;
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.TextView;
public class CustomExpandAdpter extends BaseExpandableListAdapter
{
Context context;
List< String> lst;
HashMap< String, List< String >> hm;
public CustomExpandAdpter(Context context, List
this.context=context;
this.lst=lst;
this.hm=hm;
}
// Gets the number of children in a specified group. @Override
public int getGroupCount()
{
System.out.println("======= lst size***** :======="+lst.size());
return lst.size();
}
//Gets the data associated with the given group.
@Override
public int getChildrenCount(int groupPosition) {
System.out.println("=====group position====="+groupPosition);
System.out.println("========lst get======");
System.out.println("============child sizeeeeee=========="+hm.get(lst.get(groupPosition)).size());
return hm.get(lst.get(groupPosition)).size();
}
//Gets the data associated with the given child within the given group.
@Override
public Object getGroup(int groupPosition) {
return lst.get(groupPosition);
}
//Gets the data associated with the given child within the given group.
@Override
public Object getChild(int groupPosition, int childPosition)
{
System.out.println("======getchild========"+hm.get(lst.get(groupPosition)).get(childPosition));
return hm.get(lst.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
} //this method is use for view parent that you want to expand.
@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
String grouptext=(String)getGroup(groupPosition);
if (convertView==null) {
LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=layoutInflater.inflate(R.layout.list_group, null);
} TextView t=(TextView)convertView.findViewById(R.id.lblListHeader);
t.setTypeface(null, Typeface.BOLD);
t.setText(grouptext);
return convertView;
}
@Override
// This method is use for dispayling group of child for apporopriate parent.
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
String childtxt=(String)getChild(groupPosition, childPosition);
System.out.println("=============child text is:="+childtxt);
if (convertView==null) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.list_childitem,null);
}
TextView tw=(TextView)convertView.findViewById(R.id.lblListItem);
tw.setText(childtxt);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
STEP :- 8 Now Run this application in your Android Emulator .
if you have any question regard to expandable view than you can ask i will help out.
ReplyDeletethanks