FileDocCategorySizeDatePackage
DatePicker.javaAPI DocAndroid 1.5 API10412Wed May 06 22:41:56 BST 2009android.widget

DatePicker

public class DatePicker extends FrameLayout
A view for selecting a month / year / day based on a calendar like layout. For a dialog using this view, see {@link android.app.DatePickerDialog}.

Fields Summary
private static final int
DEFAULT_START_YEAR
private static final int
DEFAULT_END_YEAR
private final com.android.internal.widget.NumberPicker
mDayPicker
private final com.android.internal.widget.NumberPicker
mMonthPicker
private final com.android.internal.widget.NumberPicker
mYearPicker
private OnDateChangedListener
mOnDateChangedListener
How we notify users the date has changed.
private int
mDay
private int
mMonth
private int
mYear
Constructors Summary
public DatePicker(android.content.Context context)


                   
       

                                                        
                
    

       
        this(context, null);
    
public DatePicker(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, 0);
    
public DatePicker(android.content.Context context, android.util.AttributeSet attrs, int defStyle)

        super(context, attrs, defStyle);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.date_picker, this, true);

        mDayPicker = (NumberPicker) findViewById(R.id.day);
        mDayPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
        mDayPicker.setSpeed(100);
        mDayPicker.setOnChangeListener(new OnChangedListener() {
            public void onChanged(NumberPicker picker, int oldVal, int newVal) {
                mDay = newVal;
                if (mOnDateChangedListener != null) {
                    mOnDateChangedListener.onDateChanged(DatePicker.this, mYear, mMonth, mDay);
                }
            }
        });
        mMonthPicker = (NumberPicker) findViewById(R.id.month);
        mMonthPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
        DateFormatSymbols dfs = new DateFormatSymbols();
        mMonthPicker.setRange(1, 12, dfs.getShortMonths());
        mMonthPicker.setSpeed(200);
        mMonthPicker.setOnChangeListener(new OnChangedListener() {
            public void onChanged(NumberPicker picker, int oldVal, int newVal) {
                
                /* We display the month 1-12 but store it 0-11 so always
                 * subtract by one to ensure our internal state is always 0-11
                 */
                mMonth = newVal - 1;
                if (mOnDateChangedListener != null) {
                    mOnDateChangedListener.onDateChanged(DatePicker.this, mYear, mMonth, mDay);
                }
                updateDaySpinner();
            }
        });
        mYearPicker = (NumberPicker) findViewById(R.id.year);
        mYearPicker.setSpeed(100);
        mYearPicker.setOnChangeListener(new OnChangedListener() {
            public void onChanged(NumberPicker picker, int oldVal, int newVal) {
                mYear = newVal;
                if (mOnDateChangedListener != null) {
                    mOnDateChangedListener.onDateChanged(DatePicker.this, mYear, mMonth, mDay);
                }
            }
        });
        
        // attributes
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DatePicker);

        int mStartYear = a.getInt(R.styleable.DatePicker_startYear, DEFAULT_START_YEAR);
        int mEndYear = a.getInt(R.styleable.DatePicker_endYear, DEFAULT_END_YEAR);
        mYearPicker.setRange(mStartYear, mEndYear);
        
        a.recycle();
        
        // initialize to current date
        Calendar cal = Calendar.getInstance();
        init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null);
        
        // re-order the number pickers to match the current date format
        reorderPickers();
        
        if (!isEnabled()) {
            setEnabled(false);
        }
    
Methods Summary
protected voiddispatchRestoreInstanceState(android.util.SparseArray container)
Override so we are in complete control of save / restore for this widget.

    


                       
    
        
        dispatchThawSelfOnly(container);
    
public intgetDayOfMonth()

        return mDay;
    
public intgetMonth()

        return mMonth;
    
public intgetYear()

        return mYear;
    
public voidinit(int year, int monthOfYear, int dayOfMonth, android.widget.DatePicker$OnDateChangedListener onDateChangedListener)
Initialize the state.

param
year The initial year.
param
monthOfYear The initial month.
param
dayOfMonth The initial day of the month.
param
onDateChangedListener How user is notified date is changed by user, can be null.

        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        mOnDateChangedListener = onDateChangedListener;
        updateSpinners();
    
protected voidonRestoreInstanceState(android.os.Parcelable state)

        SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        mYear = ss.getYear();
        mMonth = ss.getMonth();
        mDay = ss.getDay();
    
protected android.os.ParcelableonSaveInstanceState()

        Parcelable superState = super.onSaveInstanceState();
        
        return new SavedState(superState, mYear, mMonth, mDay);
    
private voidreorderPickers()

        char[] order = DateFormat.getDateFormatOrder(mContext);
        
        /* Default order is month, date, year so if that's the order then
         * do nothing.
         */
        if ((order[0] == DateFormat.MONTH) && (order[1] == DateFormat.DATE)) {
            return;
        }
        
        /* Remove the 3 pickers from their parent and then add them back in the
         * required order.
         */
        LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
        parent.removeAllViews();
        for (char c : order) {
            if (c == DateFormat.DATE) {
                parent.addView(mDayPicker);
            } else if (c == DateFormat.MONTH) {
                parent.addView(mMonthPicker);
            } else {
                parent.addView (mYearPicker);
            }
        }
    
public voidsetEnabled(boolean enabled)

        super.setEnabled(enabled);
        mDayPicker.setEnabled(enabled);
        mMonthPicker.setEnabled(enabled);
        mYearPicker.setEnabled(enabled);
    
public voidupdateDate(int year, int monthOfYear, int dayOfMonth)

        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateSpinners();
    
private voidupdateDaySpinner()

        Calendar cal = Calendar.getInstance();
        cal.set(mYear, mMonth, mDay);
        int max = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        mDayPicker.setRange(1, max);
        mDayPicker.setCurrent(mDay);
    
private voidupdateSpinners()

        updateDaySpinner();
        mYearPicker.setCurrent(mYear);
        
        /* The month display uses 1-12 but our internal state stores it
         * 0-11 so add one when setting the display.
         */
        mMonthPicker.setCurrent(mMonth + 1);