Home >> Miscellaneous
This article will try to cover aspects of creating Expandable List at runtime.
By "runtime", I mean to say not by using Android XML file for layout and Views
but with the help of coding using Android API such as
"android.app.ExpandableListActivity"
"android.widget.BaseExpandableListAdapter"
After preparing/setting-up Software Environment for developing Mobile Application on
Android Platform, for me initial task is to formulate idea as to how this application
is going to behave at run-time. For example I choose to use Expandable List View
for showing various categories/varieties of items within each group in form of List.
I choose to show "cup-boards" and "Cameras" as shopping items on display within this
ListView.On click of each item there will be sub/child elements/items getting
displayed below the main selectable section/header.
Following images are going to show the actual screen that is expected at the time of
execution of this example source code:
Initial screen on invocation of this application:
On click/touch of the first category following image shows the sub-elements:
On click/touch of the second/subsequent category following image shows
the sub-elements:
On click/touch of the both categories following image shows the sub-elements:
Let us go through the files used as far as this example is concerned:
Example Project on Android Platform main.xml file:
<?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:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"/>
</LinearLayout>
|
ExpandableListViewExample.java
package com.techienjoy.example.expandablelistview;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ExpandableListViewExample extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ExampleAdapter(this));
}
private class ExampleAdapter extends BaseExpandableListAdapter {
private Context context;
public ExampleAdapter(Context context) {
this.context = context;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
/**
* getChildView overridden method will have responsibility of
* constructing View for the child element when corresponding
* group element is activated by click / touch action.
*/
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout linear = new LinearLayout(this.context);
ImageView imView1 = null;
ImageView imView2 = null;
ImageView imView3 = null;
ImageView imView4 = null;
if(groupPosition == 0) {
imView1 = new ImageView(this.context);
imView1.setImageResource(R.drawable.cupboards1);
imView2 = new ImageView(this.context);
imView2.setImageResource(R.drawable.cupboards2);
imView3 = new ImageView(this.context);
imView3.setImageResource(R.drawable.cupboards3);
imView4 = new ImageView(this.context);
imView4.setImageResource(R.drawable.cupboards4);
linear.addView(imView1);
linear.addView(imView2);
linear.addView(imView3);
linear.addView(imView4);
}
if(groupPosition == 1) {
imView1 = new ImageView(this.context);
imView1.setImageResource(R.drawable.camera1);
imView2 = new ImageView(this.context);
imView2.setImageResource(R.drawable.camera2);
imView3 = new ImageView(this.context);
imView3.setImageResource(R.drawable.camera3);
imView4 = new ImageView(this.context);
imView4.setImageResource(R.drawable.camera4);
linear.addView(imView1);
linear.addView(imView2);
linear.addView(imView3);
linear.addView(imView4);
}
return linear;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public int getGroupCount() {
return 2;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout linear = new LinearLayout(this.context);
ImageView imView = null;
if(groupPosition == 0) {
TextView txtView = new TextView(this.context);
txtView.setText("Cup Board :");
txtView.setTextColor(Color.BLACK);
linear.addView(txtView);
imView = new ImageView(this.context);
imView.setImageResource(R.drawable.cupboards);
linear.addView(imView);
}
if(groupPosition == 1) {
TextView txtView = new TextView(this.context);
txtView.setText("Cameras :");
txtView.setTextColor(Color.BLACK);
linear.addView(txtView);
imView = new ImageView(this.context);
imView.setImageResource(R.drawable.camerascategory);
linear.addView(imView);
}
return linear;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
return true;
}
}
}
|
|
|  |
|
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 and
explained with a very simple scenario
and article with 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.
|
|
|
| Android ListView Example : |
Example on Android List View
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|
| 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 Examples : |
List of ANDROid examples
with source code and output
screens captured and shown.
|
|
|
|
|
| Google GWT Example : |
Example using GWT and some design patterns and various
ways of implementing this example.
|
|
|
|
|
|
|
|
| Android Gallery Example : |
Example on Android Gallery View
explained with a very simple scenario
and appropriate screens captured and shown.
|
|
|
|
|
|
|
|
|
|
|
| Android Sensors Example : |
Example on Android Sensors Listed and
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|
|
|
|