Skip to main content

Expandable Listview Android example



Create New Android Project In Main Activity :

 package com.multilayerexpandable;  
 import java.util.ArrayList;  
 import android.app.ExpandableListActivity;  
 import android.content.Context;  
 import android.os.Bundle;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.widget.ExpandableListView;  
 import android.widget.ExpandableListView.OnChildClickListener;  
 import android.widget.Toast;  
 public class MainActivity extends ExpandableListActivity implements  
           OnChildClickListener {  
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           ExpandableListView expandbleLis = getExpandableListView();  
           expandbleLis.setDividerHeight(2);  
           expandbleLis.setGroupIndicator(null);  
           expandbleLis.setClickable(true);  
           setGroupData();  
           setChildGroupData();  
           NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem);  
           mNewAdapter  
                     .setInflater(  
                               (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),  
                               this);  
           getExpandableListView().setAdapter(mNewAdapter);  
           expandbleLis.setOnChildClickListener(this);  
      }  
      public void setGroupData() {  
           groupItem.add("Android");  
           groupItem.add("Extras");  
      }  
      ArrayList<String> groupItem = new ArrayList<String>();  
      ArrayList<Object> childItem = new ArrayList<Object>();  
      public void setChildGroupData() {  
           /**  
            * Add Data For Android 
            */  
           ArrayList<String> child = new ArrayList<String>();  
           child.add("Cupcake");  
           child.add("Donut");  
           child.add("Eclair");  
           child.add("Froyo");  
           child.add("Gingerbread");  
           child.add("Honeycomb");  
           child.add("Ice Cream Sandwich");  
           child.add("Jelly Bean");  
           childItem.add(child);  
           /**  
            * Add Data For Extras_  
            */  
           child = new ArrayList<String>();  
           child.add("Dj-Android");  
           child.add("Dhaval Sodha Parmar");  
           childItem.add(child);  
      }  
      @Override  
      public boolean onChildClick(ExpandableListView parent, View v,  
                int groupPosition, int childPosition, long id) {  
           Toast.makeText(MainActivity.this, "Clicked On Child",  
                     Toast.LENGTH_SHORT).show();  
           return true;  
      }  
 }  

Class NewAdapter.java

 package com.multilayerexpandable;  
 import java.util.ArrayList;  
 import android.app.Activity;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.ViewGroup;  
 import android.widget.BaseExpandableListAdapter;  
 import android.widget.CheckedTextView;  
 import android.widget.ImageView;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 @SuppressWarnings("unchecked")  
 public class NewAdapter extends BaseExpandableListAdapter {  
      public ArrayList<String> groupItem, tempChild;  
      public ArrayList<Object> Childtem = new ArrayList<Object>();  
      public LayoutInflater minflater;  
      public Activity activity;  
      public NewAdapter(ArrayList<String> grList, ArrayList<Object> childItem) {  
           groupItem = grList;  
           this.Childtem = childItem;  
      }  
      public void setInflater(LayoutInflater mInflater, Activity act) {  
           this.minflater = mInflater;  
           activity = act;  
      }  
      @Override  
      public Object getChild(int groupPosition, int childPosition) {  
           return null;  
      }  
      @Override  
      public long getChildId(int groupPosition, int childPosition) {  
           return 0;  
      }  
      @Override  
      public View getChildView(int groupPosition, final int childPosition,  
                boolean isLastChild, View convertView, ViewGroup parent) {  
           tempChild = (ArrayList<String>) Childtem.get(groupPosition);  
           TextView text = null;  
           ImageView img = null;  
           if (convertView == null) {  
                convertView = minflater.inflate(R.layout.childrow, null);  
           }  
           text = (TextView) convertView.findViewById(R.id.textView1);  
           img = (ImageView) convertView.findViewById(R.id.childImage);  
           text.setText(tempChild.get(childPosition));  
           if (childPosition == (tempChild.size() - 1)) {  
                img.setImageResource(R.drawable.dh);  
           }  
           if (childPosition == tempChild.size()) {  
                img.setImageResource(R.drawable.dj);  
           }  
           convertView.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     Toast.makeText(activity, tempChild.get(childPosition),  
                               Toast.LENGTH_SHORT).show();  
                }  
           });  
           return convertView;  
      }  
      @Override  
      public int getChildrenCount(int groupPosition) {  
           return ((ArrayList<String>) Childtem.get(groupPosition)).size();  
      }  
      @Override  
      public Object getGroup(int groupPosition) {  
           return null;  
      }  
      @Override  
      public int getGroupCount() {  
           return groupItem.size();  
      }  
      @Override  
      public void onGroupCollapsed(int groupPosition) {  
           super.onGroupCollapsed(groupPosition);  
      }  
      @Override  
      public void onGroupExpanded(int groupPosition) {  
           super.onGroupExpanded(groupPosition);  
      }  
      @Override  
      public long getGroupId(int groupPosition) {  
           return 0;  
      }  
      @Override  
      public View getGroupView(int groupPosition, boolean isExpanded,  
                View convertView, ViewGroup parent) {  
           if (convertView == null) {  
                convertView = minflater.inflate(R.layout.grouprow, null);  
           }  
           ((CheckedTextView) convertView).setText(groupItem.get(groupPosition));  
           ((CheckedTextView) convertView).setChecked(isExpanded);  
           return convertView;  
      }  
      @Override  
      public boolean hasStableIds() {  
           return false;  
      }  
      @Override  
      public boolean isChildSelectable(int groupPosition, int childPosition) {  
           return false;  
      }  
 }  

Grouprow.xml


 <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:id="@+id/textView1"  
   android:layout_width="wrap_content"  
   android:layout_height="60dp"  
   android:layout_marginLeft="5dp"  
   android:gravity="center_vertical"  
   android:padding="10dp"  
   android:text="@string/hello_world"  
   android:textColor="#FFFFFF"  
   android:textSelectHandleLeft="@string/hello_world"  
   android:textSize="14sp"  
   android:textStyle="bold" />  

childrow.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="40dp"  
   android:background="@android:color/black"  
   android:clickable="true"  
   android:orientation="vertical"  
   android:paddingLeft="40dp"  
   tools:context=".MainActivity" >  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="39dp"  
     android:gravity="center_vertical" >  
     <ImageView  
       android:id="@+id/childImage"  
       android:layout_width="30dp"  
       android:layout_height="30dp"  
       android:layout_margin="5dp"  
       android:background="@drawable/ic_launcher"  
       android:contentDescription="@string/hello_world" />  
     <TextView  
       android:id="@+id/textView1"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_marginLeft="5dp"  
       android:text="@string/hello_world"  
       android:textColor="#FFFFFF"  
       android:textSize="14sp"  
       android:textStyle="bold" />  
   </LinearLayout>  
 </LinearLayout>  

Android Manifest file :

 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.multilayerexpandable"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="16" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:theme="@android:style/Theme.NoTitleBar"  
       android:name="com.multilayerexpandable.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>  

Comments

  1. This code is working first time when i click on group but when i click on second group it shows abnormal behaviour as image appear in both group in different position.

    ReplyDelete
  2. There is no Toast showing in my code.
    Can you help me with that?
    Thanks

    ReplyDelete
    Replies
    1. you get any error??? if yes tem Past its below, if any other issue then show me you code

      Delete

Post a Comment

Popular posts from this blog

Android - Google MAP V2 PART 3 (add Polyline)

Download Full Code  of  PART-1 , 2 , 3 , 4 Before go ahead Please look in to PART-1 & PART-2 I have just modify Main Activity class: package com.djandroid.mapsv2; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.Polyline; import com.google.android.gms.maps.model.PolylineOptions; public class MainActivity extends FragmentActivity { private GoogleMap MAP; private boolean markClic...

Android Material Design - 2 (Good bay ActionBar, Hello ToolBar with Navigation Drawer)

hello all, after long time i have update my blog with android material Design theme, i am tying to demonstrate new things which is available with android lollipop. using different example i 'll show you, one sample app with material design which include ToolBar, Floting Actions, Tabs, Cardview, Recycle view. Download full example on  Github . In this example we will take a look at the new Actionbar replacement called Toolbar. It is introduced in Android Lollipop, API level 21 release, and hence it is available to use, out of box for the application that are targeted to 21 and above. However, as always Google provides fully supported Toolbar features to lower android os devices via AppCompact support library. In AppCompat, Toolbar is implemented in the  android.support.v7.widget.Toolbar  class. Pre-requirements: 1. Android Studio 1.0.1 (Latest while writing the post) 2. Appcombat v7 Support librar...

How To Flash / Install Firmware Samsung Galaxy Mini S5570

This Tutorial is about flashing firmware on your Samsung Galaxy Mini S5570 . I posted a step by and easy tutorials , please read complete guide once and then apply , if you skip any step then you will have bricked phone. Disclaimer:  Follow this Guide on your risk and if your device gets damaged or bricked during the process, DJ-Android is not responsible. Download required things Download ODIN v4.38   – it is a tool that is used to flash firmware on your phone Drivers –  Download  and install drivers that are required to detect your phone by pc Download  Firmware for your phone Download   TASS_v1.0.ops File An Archive (.zip / .rar) file extractor, usually all pc owners have this tool . In case you don’t have any use 7zip archive extractor Instructions to Install Firmware on Galaxy Mini S5570 First extract  SAMSUNG_USB.rar  and install drivers on pc Extract Firmware files on safe place, ...