Sie sind auf Seite 1von 13

ANDROID EXPANDABLE LISTVIEW

ExpandableListView is a type of view very much similar to a ListView but allows two levels. Basically items are grouped inside the list much like giving a category heading to a bunch of similar items and then group them by the category headings. Each group can be expanded or collapsed individually to show or hide its children. You can attach listeners to the ExpandableListView to listen for OnClick events on the Group or the individual children. In this tutorial we learn to create an ExpandableListView simulating a Department Store where products belong to a certain department. We initially load our list with some data, then dynamically add items to the list by checking for new department and add a group otherwise just add the item to the existing department. We also associate listeners to alert which Department or Product was clicked on. We also learn how to programmatically expand and collapse all groups or just one at a time. If you have worked with ListView then it's not that complicated either. All you have to do is extend the BaseExpandableListAdapter and then attach that to the ExpandableListView. In the BaseExpandableListAdapter we have to custom code 2 methods getChildView() and getGroupView() to display our group information and the child row information. You can create custom layouts for each one and then inflate them using LayoutInflater. Note:--Here ExpandableListActivity is inherited instead of Activity class ,because ExpandableListActivity class have property of patent tag and child tag. Here we also extends class BaseExpandableListAdapter.for this reason somy many methods are overrides. public int getGroupCount()------method use for count the parent tag. public int getChildrenCount()-----method used for count the child for respective parent tag. public View getGroupView()------method used for create views of parent tag. public View getChildView()------method used for create views of child tag of respective parent tag.

SOURCE CODE ::main.xml is :<?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:orientation="vertical" >

<ExpandableListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"/> </LinearLayout>

ExpandableListViewActivity.java is :package com.demo.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 ExpandableListViewActivity extends ExpandableListActivity { /** Called when the activity is first created. */

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; } public Object getChild(int groupPosition, int childPosition) { return null; } 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. */ 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; 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);

linear.addView(imView1); linear.addView(imView2); linear.addView(imView3); } 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);

linear.addView(imView1); linear.addView(imView2); linear.addView(imView3); } return linear; } public int getChildrenCount(int groupPosition) { return 1; } public Object getGroup(int groupPosition) { return null; }

public int getGroupCount() { return 2; } public long getGroupId(int groupPosition) { return 0; } 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; } public boolean hasStableIds() { return false; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } }

Output will be :-

Android Number Picker :Class Overview : Android Number Picker Class is included in android. widget package. A Widget that enable the user to select a number forms a predefined range. There are two flavors of this widget and which one is presented to the user dependents on the current theme. SOURCE CODE ::main.xml is :<?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:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/upButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/plus" />

<EditText android:id="@+id/numberEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/box" android:gravity="center" android:inputType="number" > <requestFocus /> </EditText> <Button android:id="@+id/downButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/minus" /> </LinearLayout>

NumberExActivity.java is :package com.android; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class NumberExActivity extends Activity { private Button upButton; private Button downButton; private EditText editText; private int uprange = 20; private int downrange = 0; private int values = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); upButton = (Button) findViewById(R.id.upButton); downButton = (Button) findViewById(R.id.downButton); editText = (EditText) findViewById(R.id.numberEditText); upButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {

downButton.setBackgroundResource(R.drawable.minus); upButton.setBackgroundResource(R.drawable.plus); if (values >= downrange && values <= uprange) values++; if (values > uprange) values = downrange; editText.setText("" + values); } }); downButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { downButton .setBackgroundResource(R.drawable.minus); upButton.setBackgroundResource(R.drawable.plus); if (values >= downrange && values <= uprange) values--; if (values < downrange) values = uprange; editText.setText(values + ""); } }); } }

Output will be :-

Android Date Picker :Class Overview ; Android Date Picker class is also included in android.widget package. This class is a widget for selecting a date.The date can be selected by a year month and day spinners or a CalanderView. The set of spinner and the calendar view are automatically synchronized. The day digit,month digit,and year digit can control by : Virtical spinner & Keyboard input The client can customize whether only the spinner or only the calendar view or both to be displayed. Also the minimal and maximum date from which date to be selected can be customaries. Note:final Calendar c = Calendar.getInstance() -----method used for get system date.

When we create datepicker bdialog listener by this way----private DatePickerDialog.OnDateSetListener myDateSetListener= new DatePickerDialog.OnDateSetListener(); Then at Dateset() method we get the day,month and year as parameter.

SOURCE CODE ::main.xml is :<?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:orientation="vertical" android:background="@android:color/background_light" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/datepickerbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="DatePicker" /> </LinearLayout>

AndroidDatePickerActivity.java is :package com.android.datepicker; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.Toast; public class AndroidDatePickerActivity extends Activity { private int myYear, myMonth, myDay; static final int ID_DATEPICKER = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button datePickerButton = (Button)findViewById(R.id.datepickerbutton); datePickerButton.setOnClickListener(datePickerButtonOnClickListener); } private Button.OnClickListener datePickerButtonOnClickListener = new Button.OnClickListener(){

public void onClick(View v) { // TODO Auto-generated method stub final Calendar c = Calendar.getInstance(); myYear = c.get(Calendar.YEAR); myMonth = c.get(Calendar.MONTH); myDay = c.get(Calendar.DAY_OF_MONTH); showDialog(ID_DATEPICKER); } }; @Override protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub switch(id){ case ID_DATEPICKER: Toast.makeText(AndroidDatePickerActivity.this, "- onCreateDialog -",

Toast.LENGTH_LONG).show(); return new DatePickerDialog(this, myDateSetListener, myYear, myMonth, myDay); default: return null; } } private DatePickerDialog.OnDateSetListener myDateSetListener = new DatePickerDialog.OnDateSetListener(){ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub String date = "Year: " + String.valueOf(year) + "\n" + "Month: " + String.valueOf(monthOfYear+1) + "\n" + "Day: " + String.valueOf(dayOfMonth); Toast.makeText(AndroidDatePickerActivity.this, date, Toast.LENGTH_LONG).show(); } }; }

Output will be :-

Android Time Picker :In Android, you can use android.widget.TimePicker class to render a time picker component to select hour and minute in a pre-defined user interface. In this tutorial, we show you how to render time picker component via android.widget.TimePicker in current page, and also in dialog box via android.widget.TimePicker. In addition, we also show you how to set a hour and minute in time picker component. Class Overview : Android Time Picker class included in android.widget package. This class is a widget for selecting a time.Time can be selected by a hours,minute and AM/PM mode. A view for selecting the time of day,in either 24 hours or AM/PM mode. The hour , each minute digit and AM/PM(if applicable) con be controlled by : Vertical spinners Keyboard input

Under AM/PM mode , the user can hit a , A , p or P to pick, or hit on AM/PM button by mouse the mode automatically switched from AM to PM and PM to AM. Note:-Here we implement OnTimeSetListener class to get the dialog time as hour and minute .

SOURCE CODE ::main.xml is :<?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:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/editText1" android:layout_width="200dip" android:layout_height="wrap_content" android:text=""> </EditText> <Button android:id="@+id/button1" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="TimePicker" /></LinearLayout>

DemoTimePickerActivity.java is :package com.android.DemoPicker; import java.util.StringTokenizer; import android.app.Activity; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TimePicker; import android.widget.Toast; public class DemoTimePickerActivity extends Activity { private EditText edTxt = null; private TimePickerDialog timePickDialog = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); edTxt = (EditText) findViewById(R.id.editText1); Button but1 = (Button) findViewById(R.id.button1); but1.setOnClickListener(new OnClickListener() { public void onClick(View v) { String time = edTxt.getText().toString(); if (time != null && !time.equals("")) { String Tokenizer st = new StringTokenizer(time, ":"); String timeHour = st.nextToken(); String timeMinute = st.nextToken(); timePickDialog = new TimePickerDialog(v.getContext(), (OnTimeSetListener) new TimePickHandler(), Integer.parseInt(timeHour), Integer.parseInt(timeMinute), true); } else { timePickDialog = new TimePickerDialog(v.getContext(), new TimePickHandler(), 10, 45, true); }

timePickDialog.show(); } }); } private class TimePickHandler implements OnTimeSetListener {

public void onTimeSet(TimePicker view, int hourOfDay, int minute) { edTxt.setText(hourOfDay + ":" + minute); timePickDialog.hide(); } } }

Output will be :-

Das könnte Ihnen auch gefallen