Tech I Enjoy Logo

Custom Search




  Home >> Miscellaneous
In this Web Page I shall try to walk you through an example that
I think pretty simple and straigh-forward steps on how to use
Exapandable List View from Android Platform API.

For this example to work we need appropriate environment, both
Hardware and Software.

For using Android Platform, you can refer Google's Official 
web site for Android and on how to set up approrpiate design-time
and runtime environment for compiling and running this example.

In this example I shall show a Menu/List with a single item on screen on application start-up, as shown below: On click of the Menu item, the child item associated with this would open-up, as shown below: For this example/screen to show up, we require following different pieces/files as listed below:
-> List of XML files specific to Android XML File format:
   ->res/layout/main.xml
   ->res/layout/grouplayout_collapsed.xml
   ->res/layout/childlayout.xml

-> List of Java files:
   ->src/com/tie/example/SampleExpandableListView.java
main.xml file is having the ExpandableListView tag with a LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ExpandableListView android:layout_height="fill_parent"  
                       android:id="@android:id/list" 
                       android:layout_width="fill_parent">
    </ExpandableListView>
</LinearLayout>
grouplayout_collapsed.xml file is the layout for initial menu screen.
<?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="fill_parent"
  android:background="#aabb88">

        
        <TableLayout android:id="@+id/tableLayout1" 
                     android:layout_height="fill_parent" 
                     android:layout_width="wrap_content">
            <TableRow android:id="@+id/tableRow1" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content">
              <TextView android:id="@+id/text1" 
                        android:textColor="#000000" 
                        android:layout_width="wrap_content" 
                        android:layout_height="wrap_content" 
                        android:text="Tab One"></TextView>
            </TableRow>
            <TableRow android:id="@+id/tableRow2" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content">
              <TextView android:id="@+id/text2" 
                        android:textColor="#000000" 
                        android:layout_width="wrap_content" 
                        android:layout_height="wrap_content" 
                        android:text="Tab Two"></TextView>
            </TableRow>
        
        </TableLayout>
        
</LinearLayout>
childlayout.xml file is having TextViews for showing on the expanded view for the Expandable List View.
<?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="fill_parent"
  android:background="#bb00ff">
    <TextView android:id="@+id/text5" android:textColor="#000000" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="Tab Five"></TextView>
    <TextView android:id="@+id/text6" android:textColor="#000000" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="Tab Six"></TextView>
</LinearLayout>
After designing layout related XML files, we can take a look at the Activity code for this example. SampleExpandableListView.java
package com.tie.example;

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

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

public class SampleExpandableListView extends ExpandableListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        List groupList = new ArrayList();
        Map groupData = new HashMap();
        groupData.put("text1", " Test1 ");
        groupData.put("text2", " Test2 ");
        groupList.add(groupData);
        
        List childList = new ArrayList();
        List childList1 = new ArrayList();
        Map childMap = new HashMap();
        childMap.put("text5", " Test5 ");
        childMap.put("text6", " Test6 ");
        childList1.add(childMap);
        
        childList.add(childList1);
        
        
        SimpleExpandableListAdapter listAdapter = 
        	    new SimpleExpandableListAdapter (this, 
        	    		 groupList, R.layout.grouplayout_collapsed,
        		         new String[] {"text1","text2"}, 
        		         new int[] {R.id.text1,R.id.text2},
        		         childList, R.layout.childlayout, 
        		         new String[] {"text5","text6"}, 
        		         new int[] {R.id.text5,R.id.text6});
        
        setListAdapter(listAdapter);
    }
}
This Java class extends ExpandableListActivity class file from the Android API. onCreate method is choosen for preparing data for the SimpleExpandableListAdapter instance creation. This class constructor requires ids and static values for all the TextViews. Once adapter is instantiated, it can be used by super class to substitute for the ListAdapter instance. In order to add another menu item to the expandable list I have modified onCreate method from the above class file, to look something like as follows:
. . . . .
. . . . .
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        List groupList = new ArrayList();
        Map groupData = new HashMap();
        groupData.put("text1", " Test1 ");
        groupData.put("text2", " Test2 ");

        groupList.add(groupData);
        
        Map groupData1 = new HashMap();
        groupData1.put("text1", " Test1 ");
        groupData1.put("text2", " Test2 ");
        
        groupList.add(groupData1);
        
        List childList = new ArrayList();
        List childList1 = new ArrayList();
        Map childMap = new HashMap();
        childMap.put("text5", " Test5 ");
        childMap.put("text6", " Test6 ");
        childList1.add(childMap);
        childList.add(childList1);
        
        List childList2 = new ArrayList();
        Map childMap1 = new HashMap();
        childMap1.put("text3", " Test3 ");
        childMap1.put("text4", " Test4 ");
        childList2.add(childMap1);
        
        childList.add(childList2);
        
        
        SimpleExpandableListAdapter listAdapter = 
        	    new SimpleExpandableListAdapter (this, 
        	    		 groupList, R.layout.grouplayout_collapsed,
        		         new String[] {"text1","text2"}, 
        		         new int[] {R.id.text1,R.id.text2},
        		         childList, R.layout.childlayout, 
        		         new String[] {"text3","text4","text5","text6"}, 
        		         new int[] {R.id.text3,R.id.text4,R.id.text5,R.id.text6});
        
        setListAdapter(listAdapter);
    }
. . . . .
. . . . .
The above change/modification will result in the final screen as shown below: Again I wanted to change the way child layout is aligned in this example, by using TableLayout, I have modified childlayout.xml file to achive screen that is shown as follows: childlayout.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="fill_parent"
  android:background="#bb00ff">
    
    
    <TableLayout android:id="@+id/tableLayout1" 
                 android:layout_height="fill_parent" 
                 android:layout_width="wrap_content">
        <TableRow android:layout_height="fill_parent" 
                  android:layout_width="wrap_content" 
                  android:id="@+id/tableRow1"
                  android:background="#ff0000">
            <TextView android:id="@+id/text3" android:textColor="#000000" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content" 
                      android:text="Tab Three"></TextView>
        </TableRow>
        <TableRow android:id="@+id/tableRow2" 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content"
                  android:background="#00ff00">
            <TextView android:id="@+id/text4" android:textColor="#000000" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content" 
                      android:text="Tab Four"></TextView>

        </TableRow>
    </TableLayout>

    <TableLayout android:id="@+id/tableLayout2" 
                 android:layout_height="fill_parent" 
                 android:layout_width="wrap_content">
        <TableRow android:layout_height="fill_parent" 
                  android:layout_width="wrap_content" 
                  android:id="@+id/tableRow3"
                  android:background="#ff0000">
          <TextView android:id="@+id/text5" android:textColor="#000000" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="Tab Five"></TextView>
        </TableRow>
        <TableRow android:id="@+id/tableRow4" 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content"
                  android:background="#00ff00">
          <TextView android:id="@+id/text6" android:textColor="#000000" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="Tab Six"></TextView>

        </TableRow>
    </TableLayout>
  
</LinearLayout>
Let us take this example one step further with addition of an image inside the expandable list item. The image is this , and the change/modification done to accomodate this image within the TableRow tag is as follows:
    <TableRow android:layout_height="fill_parent" 
                  android:layout_width="wrap_content" 
                  android:id="@+id/tableRow1"
                  android:background="#ff0000">
            <TextView android:id="@+id/text3" android:textColor="#000000" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content" 
                      android:text="Tab Three"></TextView>
            <ImageView android:id="@+id/image1" 
                       android:src="@drawable/multicolor"/>
    </TableRow>
The image used by this ImageView should be placed in the res/drawable/ folder within the Android Project in Eclipse example workspace. At run-time this example would look something like as follows: If you would like to share your thoughts on this, please write to us @ usingframeworks @ gmail dot com If anything missed out , please let me know at techienjoy at yahoo . com
Android ListView Example :
Example on Android ListView 
explained with a very simple scenario
whereby showing folder and files with
structure and appropriate screens 
captured and shown.
Android Tab View Example :
Example on Android Tab View
explained with a very simple scenario
and appropriate screens captured and shown.
Example of using Log4J Part 2 :
Log4j example with source code on Java Platform.
DOJO Dialog Example :
Example on using DOJO Dialog
explained with a very simple scenario
Google GWT Example :
Example using GWT and some design patterns and various
ways of implementing this example.
Wizard Framework using Java Platform :
Example using Custom Wizard Framework 
with code and explained
Android ListView Example :
Example on Android List View
explained with a very simple scenario 
and article with appropriate screens 
captured and shown.
Web Load Test with example :
Example using Load test functionalities 
with code and explained
Android Layout Example :
Android Example on using Layout
with source code Explained.
Android Gallery with SurfaceView :
Example showing Android Gallery
with SurfaceView and Spinner
Android Example on Expandable ListView :
Example on using Expandable ListView
on Android Platform.A step by step source code
explained.
Android Sensors Example :
Example on Android Sensors Listed and
explained with a very simple scenario 
and article with appropriate screens 
captured and shown.
JSF example with source code :
JSF example with source code on Java Platform.
Using Apache Commons Log With Example :
Example using Apache commons log 
with code and explained
Android Example on Expandable List :
Example on using Expandable ListView
on Android Platform.A step by step source code
explained.
Android ListView Example :
Example on Android ListView and
explained with a very simple scenario 
and article with appropriate screens 
captured and shown.
Example of using Log4J Part 1 :
Log4j example with source code on Java Platform.
Using Different Logger Files :
Example on using different log files 
using Apache Log4j Framework.
Example of using Mule ESB JMS Transport :
Example of using Mule ESB JMS Transport with simple
to explain source code.
Android Menu and MenuItem Example :
Example using Menu and MenuItem using Android Platform 
with code and explained
JSF example with source code :
JSF example of Tags and Data Table 
with source code on Java Platform.
Android DatePickerDialog Example :
Example on Android DatePickerDialog
explained with a very simple scenario
and appropriate screens captured and shown.
Using Quartz Scheduler Example :
Example on how to use Quartz Scheduler.
JSF example with source code :
JSF example of Tags and SelectBoxes 
with source code on Java Platform.
JSF example on validation :
JSF Validation with example with source code 
on Java Platform.
List of Examples on Various Technologies :
List of Examples on Various Technologies and Frameworks.
Android Text to Speech Example :
Android Example on using Text
2 Speech conversion explained with
source code Explained.
Android Image Gradient Merge :
Example using Images and Gradient Shape using
Android Platform.
Android Examples :
List of ANDROid examples
with source code and output
screens captured and shown.
Android Example on Downloading AnyFormat :
Example on ways to download any file with
any format using Android Platform.
Android Gallery Example Enhanced :
Example on Android Gallery View
explained with a very simple scenario
and appropriate screens captured and shown.
Android ImageView Example :
Example on using ImageView using 
Android Platform. A very simple to setup
and see it working.
JSF example with source code :
JSF example of Tags and checkboxes 
with source code on Java Platform.
Example using Tag Library :
Example on how to code and use
Custom Tag Library on Java Platform.
JDBC Transaction Isolation Levels :
A short write-up on JDBC Transaction
Isolation showing ways to achieve
various Isolation levels using JDBC.
Android TimePickerDialog Example :
Example on Android TimePickerDialog
explained with a very simple scenario
and appropriate screens captured and shown.
DOJO Tree Widget Example :
Example on using DOJO Tree Widget
explained with a very simple scenario
JSF example with source code :
JSF example of Tags and Code Walk-through 
with source code on Java Platform.
Android Gallery Example :
Example on Android Gallery View
explained with a very simple scenario
and appropriate screens captured and shown.
JSF example on Resource Bundle :
JSF example of Resource Bundle with source code 
on Java Platform.
Example of using Mule ESB File Transport :
Example of using Mule ESB File Transport with simple
to explain source code.


References :
Tags: TabHost and TabActivity Example on Android Platform
Tags: ListView Example on Android Platform
Tags: android sensors list
Tags: android listview example
Tags: android imageview example
Tags: Android example download any file sourcecode
Tags: android expandable list dynamically created example
Tags: android expandable list example
Tags: Android Gallery surfaceviews spinner
Tags: Android example download any file sourcecode
Tags: Android Layout Example
Tags: Android Text To Speech Example

Tags: DOJO Example Dialog
Tags: DOJO Example Tree Widget
Tags: different logger file log4j
Tags: JDBC Transaction isolation
Tags: event handling java code
Tags: example quartz scheduler
Tags: example tag library web application
Tags: Flex
Tags: index
Tags: inmemory image creation java awt
Tags: JSF Example Main
Tags: JSF Example Tags CheckBoxes
Tags: JSF Example Tags dataTable
Tags: JSF Example Tags SelectBoxes
Tags: JSF Example Tags Walkthrough
Tags: JSF Example Validation
Tags: JSF Resource Bundle
Tags: log4j example 1
Tags: log4j example
Tags: Miscellaneous
Tags: Mule ESB File Transport
Tags: Mule ESB JMS Transport
Tags: stream download batch
Tags: sychronized block wait notify
Tags: thread wait notify example
Tags: using apache commons log
Tags: web load test
Tags: Wizard Framework Idea Java



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions