FileDocCategorySizeDatePackage
DatePickerDialog.javaAPI DocAndroid 1.5 API6818Wed May 06 22:41:54 BST 2009android.app

DatePickerDialog

public class DatePickerDialog extends AlertDialog implements android.widget.DatePicker.OnDateChangedListener, android.content.DialogInterface.OnClickListener
A simple dialog containing an {@link android.widget.DatePicker}.

Fields Summary
private static final String
YEAR
private static final String
MONTH
private static final String
DAY
private final android.widget.DatePicker
mDatePicker
private final OnDateSetListener
mCallBack
private final Calendar
mCalendar
private final DateFormat
mDateFormat
private final DateFormat
mTitleDateFormat
private final String[]
mWeekDays
private int
mInitialYear
private int
mInitialMonth
private int
mInitialDay
Constructors Summary
public DatePickerDialog(android.content.Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

param
context The context the dialog is to run in.
param
callBack How the parent is notified that the date is set.
param
year The initial year of the dialog.
param
monthOfYear The initial month of the dialog.
param
dayOfMonth The initial day of the dialog.


                      
       

                                                        
                
    

                                                       
      
             
             
             
              
        this(context, com.android.internal.R.style.Theme_Dialog_Alert, 
                callBack, year, monthOfYear, dayOfMonth);
    
public DatePickerDialog(android.content.Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

param
context The context the dialog is to run in.
param
theme the theme to apply to this dialog
param
callBack How the parent is notified that the date is set.
param
year The initial year of the dialog.
param
monthOfYear The initial month of the dialog.
param
dayOfMonth The initial day of the dialog.

        super(context, theme);

        mCallBack = callBack;
        mInitialYear = year;
        mInitialMonth = monthOfYear;
        mInitialDay = dayOfMonth;
        DateFormatSymbols symbols = new DateFormatSymbols();
        mWeekDays = symbols.getShortWeekdays();
        
        mDateFormat = DateFormat.getMediumDateFormat(context);
        mTitleDateFormat = java.text.DateFormat.
                                getDateInstance(java.text.DateFormat.FULL);
        mCalendar = Calendar.getInstance();
        updateTitle(mInitialYear, mInitialMonth, mInitialDay);
        
        setButton(context.getText(R.string.date_time_set), this);
        setButton2(context.getText(R.string.cancel), (OnClickListener) null);
        setIcon(R.drawable.ic_dialog_time);
        
        LayoutInflater inflater = 
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.date_picker_dialog, null);
        setView(view);
        mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
        mDatePicker.init(mInitialYear, mInitialMonth, mInitialDay, this);
    
Methods Summary
public voidonClick(android.content.DialogInterface dialog, int which)

        if (mCallBack != null) {
            mDatePicker.clearFocus();
            mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(), 
                    mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
        }
    
public voidonDateChanged(android.widget.DatePicker view, int year, int month, int day)

        updateTitle(year, month, day);
    
public voidonRestoreInstanceState(android.os.Bundle savedInstanceState)

        super.onRestoreInstanceState(savedInstanceState);
        int year = savedInstanceState.getInt(YEAR);
        int month = savedInstanceState.getInt(MONTH);
        int day = savedInstanceState.getInt(DAY);
        mDatePicker.init(year, month, day, this);
        updateTitle(year, month, day);
    
public android.os.BundleonSaveInstanceState()

        Bundle state = super.onSaveInstanceState();
        state.putInt(YEAR, mDatePicker.getYear());
        state.putInt(MONTH, mDatePicker.getMonth());
        state.putInt(DAY, mDatePicker.getDayOfMonth());
        return state;
    
public voidshow()

        super.show();

        /* Sometimes the full month is displayed causing the title
         * to be very long, in those cases ensure it doesn't wrap to
         * 2 lines (as that looks jumpy) and ensure we ellipsize the end.
         */
        TextView title = (TextView) findViewById(R.id.alertTitle);
        title.setSingleLine();
        title.setEllipsize(TruncateAt.END);
    
public voidupdateDate(int year, int monthOfYear, int dayOfMonth)

        mInitialYear = year;
        mInitialMonth = monthOfYear;
        mInitialDay = dayOfMonth;
        mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
    
private voidupdateTitle(int year, int month, int day)

        mCalendar.set(Calendar.YEAR, year);
        mCalendar.set(Calendar.MONTH, month);
        mCalendar.set(Calendar.DAY_OF_MONTH, day);
        setTitle(mTitleDateFormat.format(mCalendar.getTime()));