Android

Android Expandable List Dynamically Created with an example
Author : Ishtek

Using Android API Level : 7 
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 interested in commenting related to Android Expandable ListView Example
Click Here

Name (To be shown on this Page:*

Email (Not to be shown):*

Website (To be shown on this Page)

Your Comments here (To be shown on this Page):*

(You can use <a> Tag in your comments)

Please enter same number as shown here 436

Name: usingframeworksDated: 2013-10-21 21:42:54
Nice example... thanks !!!    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 627

Name: GirishDated: 2013-12-10 09:31:42
How to code expandable listview in a WebView as UI component?

Is there any ways to use UI widget in WebView, or one has to use only HTML, Javascript for coding such
UI to execute in WebView component.    
Name: IshtekDated: 2014-01-03 20:02:11
I don't think, Android WebView could take any other view like ExpandableListView as child element.

Android WebView is having WebKit Rendering engine as the displaying browser, so it could display code 
that uses Web technologies like HTML, CSS, JavaScript etc.

If you really wanted a look and feel of that of the ExpandableListView element, then best to try code
the same using JavaScript framework, HTML, CSS,
and you can refer HTML5 meta tag such as 'viewport' and rendering screen width dynamically at runtime.

Hope this helps.

Ishtek
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 627

Name: UsingframeworksDated: 2014-01-09 14:00:27
What if the child item be an instance of WebView element from Android Platform?
Is there any approach for accomodating lag in page or element render depending on the network bandwith and server latency for the page.

Thanks in advance for any help.    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 627

Name: madhukarDated: 2015-08-31 18:32:58
hi can help me to write json data into expandable listview ,i am getting data through server i am unable to get the coding 

please please  help me     
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 627

Name: NestorDated: 2015-12-16 13:24:02
How to respond to onclick of child elements    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 627




Android Thread Example

REST Interview Questions

JAX-WS Webservice Example

Android Overlap SurfaceView Example

Spring3 Integration of File Endpoint

JAXWS Webservice Example

Android WebView Example

Android Context Menu Example

Setting up Eclipse for Android App Development

Android SensorEvent Orientation Example

Android ImageSwitcher Example

REST API Design for Android App

Android ExpandableListView Example

Singleton Serializable on Android

Android GPS with Google MAP Example

Android Project With Example

Android Project Manageing Files

Project Ideas for App on Android Platform

Android EditText with Background

Android SQLite Database Example

Android AutoCompleteTextView Example

Android Tester

Android Interview Questions

Android ListView Example

Android Intent Notification Example

Android Intent Broadcast Example

Android TextView Link Example

Android Sensor List

Android Sensor Orientation Example

Android Intent Broadcast Receiver

Android Gallery SurfaceViews Example

Android Location Example

Android Text to Speech Example

Android Intent Example

Android Layout Example

Android Download File Example

Android Expandable ListView Example

Android Tab Example

Android RelativeLayout Example

Android Intent Example

Android Text to Speech Example

Android SQLite Example

Android CustomView Example

Android Canvas Example

Android SharedPreferences Example

Android Shape Background Example

Android ShapeButton Example

Android ViewFlipper Example

Android Gallery Example

Android TimePickerDialog Example





If interested commenting on this Page :

comments powered by Disqus
Any source code found on any of the page(s) in Techiecommunity.net is associated with following notice: 
   /*
     * TECHIECOMMUNITY.NET MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
     * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
     * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
     * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. TECHIECOMMUNITY.NET SHALL NOT BE LIABLE FOR
     * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
     * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
     *
     * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
     * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
     * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
     * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
     * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
     * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
     * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). TECHIECOMMUNITY.NET
     * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
     * HIGH RISK ACTIVITIES.
     */
   
The content provided in this page is NOT warranted and/or NOT guaranteed by techiecommunity.net .
techiecommunity.net is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

For any of the content, if you would like to bring it to notice for removal from this web site, 
please write to this web site administrator @ EMAIL-ID (please remove space before and after @),
with appropriate concern and supporting proof(s). After thorough review and if found genuine concern,
we would take appropriate action and remove disputed content from this web site within 24 hours 
starting from the time it has brought to techiecommunity.net Administrator notice.

This website uses cookies and third party cookies to improve the user experience. 
And to provide more relevant ads. 
In case of continued use of this website implies that you accept the use of cookies on this website.
		
While using this web site, you agree to have read and accepted techiecommunity.net terms of use and privacy policy.