TimePicker and DataPicker Views
--see (Books/Android/Source Code/BasicViews4)
TimePicker (android.widget.TimePicker) is used to select a time by the user ---possibly related to some data entry
DatePicker (android.widget.DatePicker) is used to select a date by a user
- Create instances of TimePicker and DatePicker Can create TimePicker and DatePicker instances in the XML layout file (see below) OR directly in code (see next step )
- Get Handle to the TimePicker and DatePicker o the progress bar shows by default a spinning wheel --- it will start automatically -- in this case progress is just ongoing and stopped (see next)
TimePicker timePicker = new TimePicker(); //creating in code rather than XML DatePicker datePicker = new DatePicker(); //creating in code rather than XML
- Display the TimePicker and DatePicker as you wish using an android.app.TimePickerDialog and android.app.DatePickerDialog
- here we use the classes TimePickerDialog and DatePickerDialog to display the TimePicker and DatePicker
- For TimePickerDialog and DatePickerDialog Create Event Handlers extending classes android.widget.TimePickerDialog.OnTimeSetListener and android.widget.DatePickerDialog.OnTimeSetListener
- you must implement either a separate classes or anonymous classes these Listener Interfaces and register them
to your Dialogs above by passing in the instance of the listener to the dialog constructor.. - NOTE: eventhandlers recieve date/time information user selects in corresponding dialog pass by the invoked onDateSet(*) and onTimeSet(*) methods that are invoked when user picks Date and Time in corresponding Dialogs
- See full example below
- you must implement either a separate classes or anonymous classes these Listener Interfaces and register them
- Retrieve information user set in TimePicker and DatePicker using methods like datePicker.getMonth(), getDay and timePicker. getCurrentMinute()
- NOTE: eventhandlers also recieve this information in the onDateSet(*) and onTimeSet(*) methods that are invoked when user picks Date and Time in corresponding Dialogs
- EXAMPLE
- We create both code for TimePicker and DatePicker
- However, we only call showDialog on the DatePickerDialog associated with the DatePicker.
TimePicker timePicker = (TimePicker) findViewByID(R.id.timePicker); //id parameter of xml is R.id.timePicker DatePicker datePicker = (DatePicker) findViewByID(R.id.datePicker); //id parameter of xml
showDialog(new TimePickerDialog(***));
class BasicViews4Activity extends Activity
{
***********code********
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
**** your code****
//show TimePickerDialog --pass listener and variables to store time as hour,minute
showDialog(new TimePickerDialog(this, mTimeSetListener, hour, minute, false);
//show DatePickerDialog --pass listener and variables to store date as yr,month,day
showDialog(new DatePickerDialog(this, mDateSetListener, yr, month,day);
***** more code****
}
********code****
}
***********************************************
main.xml ( activities defined GUI in xml) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" <Button android:id="@+id/btnSet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I am all set!" android:onClick="onClick" /> <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> |
BasicViews4Activity.java package net.learn2develop.BasicViews4; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.view.View; import android.widget.DatePicker; import android.widget.TimePicker; import android.widget.Toast; public class BasicViews4Activity extends Activity {
TimePicker timePicker;
DatePicker datePicker;
int hour, minute; int yr, month, day; static final int TIME_DIALOG_ID = 0; static final int DATE_DIALOG_ID = 1; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//handles to timePicker and datePicker
timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
datePicker = (DatePicker) findViewById(R.id.datePicker);
//---get the current date---
Calendar today = Calendar.getInstance();
yr = today.get(Calendar.YEAR);
month = today.get(Calendar.MONTH);
day = today.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_DIALOG_ID); //show the Dialog for DateDailogPicker //could add call to showDialog(TIME_DIALOG_ID); } //called by showDialog above
@Override
protected Dialog onCreateDialog(int id)
{
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, hour, minute, false);
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, yr, month, day);
} return null; }
//instance of private anonymous class to be OnDateSetListener for our DatePickerDialog defined above
// onDateSet(*) method will be invoked when user sets Date in the corresponding DatePickerDialog
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
yr = year; //assign the user selected values passed to this method
month = monthOfYear;
day = dayOfMonth;
Toast.makeText(getBaseContext(),"You have selected : " + (month + 1) +
//instance of private anonymous class to be OnTimeSetListener for our TimePickerDialog defined above // onTimeSet(*) method will be invoked when user sets Time in the corresponding TimePickerDialog SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm aa");
Date date = new Date(0,0,0, hour, minute);
String strDate = timeFormat.format(date);
Toast.makeText(getBaseContext(), "You have selected " + strDate, Toast.LENGTH_SHORT).show();
}
};
//event handler for Button in GUI of this Activity defined in xml file --shows date and timeinfo
public void onClick(View view) {
Toast.makeText(getBaseContext(), "Date selected:" + (datePicker.getMonth() + 1) +
"/" + datePicker.getDayOfMonth() + "/" + datePicker.getYear() + "\n" +
"Time selected:" + timePicker.getCurrentHour() +
":" + timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show();
}
} |
