FileDocCategorySizeDatePackage
DateWidgets1.javaAPI DocAndroid 1.5 API4955Wed May 06 22:41:08 BST 2009com.example.android.apis.view

DateWidgets1

public class DateWidgets1 extends android.app.Activity
Basic example of using date and time widgets, including {@link android.app.TimePickerDialog} and {@link android.widget.DatePicker}. Also provides a good example of using {@link Activity#onCreateDialog}, {@link Activity#onPrepareDialog} and {@link Activity#showDialog} to have the activity automatically save and restore the state of the dialogs.

Fields Summary
private android.widget.TextView
mDateDisplay
private int
mYear
private int
mMonth
private int
mDay
private int
mHour
private int
mMinute
static final int
TIME_DIALOG_ID
static final int
DATE_DIALOG_ID
private DatePickerDialog.OnDateSetListener
mDateSetListener
private TimePickerDialog.OnTimeSetListener
mTimeSetListener
Constructors Summary
Methods Summary
protected voidonCreate(android.os.Bundle savedInstanceState)


    
        
        super.onCreate(savedInstanceState);

        setContentView(R.layout.date_widgets_example_1);

        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);

        Button pickDate = (Button) findViewById(R.id.pickDate);
        pickDate.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        Button pickTime = (Button) findViewById(R.id.pickTime);
        pickTime.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });

        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        updateDisplay();
    
protected android.app.DialogonCreateDialog(int id)

        switch (id) {
            case TIME_DIALOG_ID:
                return new TimePickerDialog(this,
                        mTimeSetListener, mHour, mMinute, false);
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                            mDateSetListener,
                            mYear, mMonth, mDay);
        }
        return null;
    
protected voidonPrepareDialog(int id, android.app.Dialog dialog)

        switch (id) {
            case TIME_DIALOG_ID:
                ((TimePickerDialog) dialog).updateTime(mHour, mMinute);
                break;
            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
                break;
        }
    
private static java.lang.Stringpad(int c)


         
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    
private voidupdateDisplay()

        mDateDisplay.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append(" ")
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)));