FileDocCategorySizeDatePackage
DatePickerDialog.javaAPI DocAndroid 5.1 API19115Thu Mar 12 22:22:50 GMT 2015com.android.datetimepicker.date

DatePickerDialog

public class DatePickerDialog extends android.app.DialogFragment implements android.view.View.OnClickListener, DatePickerController
Dialog allowing users to select a date.

Fields Summary
private static final String
TAG
private static final int
UNINITIALIZED
private static final int
MONTH_AND_DAY_VIEW
private static final int
YEAR_VIEW
private static final String
KEY_SELECTED_YEAR
private static final String
KEY_SELECTED_MONTH
private static final String
KEY_SELECTED_DAY
private static final String
KEY_LIST_POSITION
private static final String
KEY_WEEK_START
private static final String
KEY_YEAR_START
private static final String
KEY_YEAR_END
private static final String
KEY_CURRENT_VIEW
private static final String
KEY_LIST_POSITION_OFFSET
private static final int
DEFAULT_START_YEAR
private static final int
DEFAULT_END_YEAR
private static final int
ANIMATION_DURATION
private static final int
ANIMATION_DELAY
private static SimpleDateFormat
YEAR_FORMAT
private static SimpleDateFormat
DAY_FORMAT
private final Calendar
mCalendar
private OnDateSetListener
mCallBack
private HashSet
mListeners
private AccessibleDateAnimator
mAnimator
private android.widget.TextView
mDayOfWeekView
private android.widget.LinearLayout
mMonthAndDayView
private android.widget.TextView
mSelectedMonthTextView
private android.widget.TextView
mSelectedDayTextView
private android.widget.TextView
mYearView
private DayPickerView
mDayPickerView
private YearPickerView
mYearPickerView
private android.widget.Button
mDoneButton
private int
mCurrentView
private int
mWeekStart
private int
mMinYear
private int
mMaxYear
private Calendar
mMinDate
private Calendar
mMaxDate
private com.android.datetimepicker.HapticFeedbackController
mHapticFeedbackController
private boolean
mDelayAnimation
private String
mDayPickerDescription
private String
mSelectDay
private String
mYearPickerDescription
private String
mSelectYear
Constructors Summary
public DatePickerDialog()


                      
       

                                                                  
                
    

                        
       

          
    


      
        // Empty constructor required for dialog fragment.
    
Methods Summary
private voidadjustDayInMonthIfNeeded(int month, int year)

        int day = mCalendar.get(Calendar.DAY_OF_MONTH);
        int daysInMonth = Utils.getDaysInMonth(month, year);
        if (day > daysInMonth) {
            mCalendar.set(Calendar.DAY_OF_MONTH, daysInMonth);
        }
    
public intgetFirstDayOfWeek()

        return mWeekStart;
    
public java.util.CalendargetMaxDate()

return
The maximal date supported by this DatePicker. Null if it has not been set.

        return mMaxDate;
    
public intgetMaxYear()

        return mMaxYear;
    
public java.util.CalendargetMinDate()

return
The minimal date supported by this DatePicker. Null if it has not been set.

        return mMinDate;
    
public intgetMinYear()

        return mMinYear;
    
public com.android.datetimepicker.date.MonthAdapter.CalendarDaygetSelectedDay()

        return new CalendarDay(mCalendar);
    
public voidinitialize(com.android.datetimepicker.date.DatePickerDialog$OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

        mCallBack = callBack;
        mCalendar.set(Calendar.YEAR, year);
        mCalendar.set(Calendar.MONTH, monthOfYear);
        mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    
public static com.android.datetimepicker.date.DatePickerDialognewInstance(com.android.datetimepicker.date.DatePickerDialog$OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

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.

        DatePickerDialog ret = new DatePickerDialog();
        ret.initialize(callBack, year, monthOfYear, dayOfMonth);
        return ret;
    
public voidonClick(android.view.View v)

        tryVibrate();
        if (v.getId() == R.id.date_picker_year) {
            setCurrentView(YEAR_VIEW);
        } else if (v.getId() == R.id.date_picker_month_and_day) {
            setCurrentView(MONTH_AND_DAY_VIEW);
        }
    
public voidonCreate(android.os.Bundle savedInstanceState)

        super.onCreate(savedInstanceState);
        final Activity activity = getActivity();
        activity.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        if (savedInstanceState != null) {
            mCalendar.set(Calendar.YEAR, savedInstanceState.getInt(KEY_SELECTED_YEAR));
            mCalendar.set(Calendar.MONTH, savedInstanceState.getInt(KEY_SELECTED_MONTH));
            mCalendar.set(Calendar.DAY_OF_MONTH, savedInstanceState.getInt(KEY_SELECTED_DAY));
        }
    
public android.view.ViewonCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container, android.os.Bundle savedInstanceState)

        Log.d(TAG, "onCreateView: ");
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        View view = inflater.inflate(R.layout.date_picker_dialog, null);

        mDayOfWeekView = (TextView) view.findViewById(R.id.date_picker_header);
        mMonthAndDayView = (LinearLayout) view.findViewById(R.id.date_picker_month_and_day);
        mMonthAndDayView.setOnClickListener(this);
        mSelectedMonthTextView = (TextView) view.findViewById(R.id.date_picker_month);
        mSelectedDayTextView = (TextView) view.findViewById(R.id.date_picker_day);
        mYearView = (TextView) view.findViewById(R.id.date_picker_year);
        mYearView.setOnClickListener(this);

        int listPosition = -1;
        int listPositionOffset = 0;
        int currentView = MONTH_AND_DAY_VIEW;
        if (savedInstanceState != null) {
            mWeekStart = savedInstanceState.getInt(KEY_WEEK_START);
            mMinYear = savedInstanceState.getInt(KEY_YEAR_START);
            mMaxYear = savedInstanceState.getInt(KEY_YEAR_END);
            currentView = savedInstanceState.getInt(KEY_CURRENT_VIEW);
            listPosition = savedInstanceState.getInt(KEY_LIST_POSITION);
            listPositionOffset = savedInstanceState.getInt(KEY_LIST_POSITION_OFFSET);
        }

        final Activity activity = getActivity();
        mDayPickerView = new SimpleDayPickerView(activity, this);
        mYearPickerView = new YearPickerView(activity, this);

        Resources res = getResources();
        mDayPickerDescription = res.getString(R.string.day_picker_description);
        mSelectDay = res.getString(R.string.select_day);
        mYearPickerDescription = res.getString(R.string.year_picker_description);
        mSelectYear = res.getString(R.string.select_year);

        mAnimator = (AccessibleDateAnimator) view.findViewById(R.id.animator);
        mAnimator.addView(mDayPickerView);
        mAnimator.addView(mYearPickerView);
        mAnimator.setDateMillis(mCalendar.getTimeInMillis());
        // TODO: Replace with animation decided upon by the design team.
        Animation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(ANIMATION_DURATION);
        mAnimator.setInAnimation(animation);
        // TODO: Replace with animation decided upon by the design team.
        Animation animation2 = new AlphaAnimation(1.0f, 0.0f);
        animation2.setDuration(ANIMATION_DURATION);
        mAnimator.setOutAnimation(animation2);

        mDoneButton = (Button) view.findViewById(R.id.done);
        mDoneButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tryVibrate();
                if (mCallBack != null) {
                    mCallBack.onDateSet(DatePickerDialog.this, mCalendar.get(Calendar.YEAR),
                            mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
                }
                dismiss();
            }
        });

        updateDisplay(false);
        setCurrentView(currentView);

        if (listPosition != -1) {
            if (currentView == MONTH_AND_DAY_VIEW) {
                mDayPickerView.postSetSelection(listPosition);
            } else if (currentView == YEAR_VIEW) {
                mYearPickerView.postSetSelectionFromTop(listPosition, listPositionOffset);
            }
        }

        mHapticFeedbackController = new HapticFeedbackController(activity);
        return view;
    
public voidonDayOfMonthSelected(int year, int month, int day)

        mCalendar.set(Calendar.YEAR, year);
        mCalendar.set(Calendar.MONTH, month);
        mCalendar.set(Calendar.DAY_OF_MONTH, day);
        updatePickers();
        updateDisplay(true);
    
public voidonPause()

        super.onPause();
        mHapticFeedbackController.stop();
    
public voidonResume()

        super.onResume();
        mHapticFeedbackController.start();
    
public voidonSaveInstanceState(android.os.Bundle outState)

        super.onSaveInstanceState(outState);
        outState.putInt(KEY_SELECTED_YEAR, mCalendar.get(Calendar.YEAR));
        outState.putInt(KEY_SELECTED_MONTH, mCalendar.get(Calendar.MONTH));
        outState.putInt(KEY_SELECTED_DAY, mCalendar.get(Calendar.DAY_OF_MONTH));
        outState.putInt(KEY_WEEK_START, mWeekStart);
        outState.putInt(KEY_YEAR_START, mMinYear);
        outState.putInt(KEY_YEAR_END, mMaxYear);
        outState.putInt(KEY_CURRENT_VIEW, mCurrentView);
        int listPosition = -1;
        if (mCurrentView == MONTH_AND_DAY_VIEW) {
            listPosition = mDayPickerView.getMostVisiblePosition();
        } else if (mCurrentView == YEAR_VIEW) {
            listPosition = mYearPickerView.getFirstVisiblePosition();
            outState.putInt(KEY_LIST_POSITION_OFFSET, mYearPickerView.getFirstPositionOffset());
        }
        outState.putInt(KEY_LIST_POSITION, listPosition);
    
public voidonYearSelected(int year)

        adjustDayInMonthIfNeeded(mCalendar.get(Calendar.MONTH), year);
        mCalendar.set(Calendar.YEAR, year);
        updatePickers();
        setCurrentView(MONTH_AND_DAY_VIEW);
        updateDisplay(true);
    
public voidregisterOnDateChangedListener(com.android.datetimepicker.date.DatePickerDialog$OnDateChangedListener listener)

        mListeners.add(listener);
    
private voidsetCurrentView(int viewIndex)

        long millis = mCalendar.getTimeInMillis();

        switch (viewIndex) {
            case MONTH_AND_DAY_VIEW:
                ObjectAnimator pulseAnimator = Utils.getPulseAnimator(mMonthAndDayView, 0.9f,
                        1.05f);
                if (mDelayAnimation) {
                    pulseAnimator.setStartDelay(ANIMATION_DELAY);
                    mDelayAnimation = false;
                }
                mDayPickerView.onDateChanged();
                if (mCurrentView != viewIndex) {
                    mMonthAndDayView.setSelected(true);
                    mYearView.setSelected(false);
                    mAnimator.setDisplayedChild(MONTH_AND_DAY_VIEW);
                    mCurrentView = viewIndex;
                }
                pulseAnimator.start();

                int flags = DateUtils.FORMAT_SHOW_DATE;
                String dayString = DateUtils.formatDateTime(getActivity(), millis, flags);
                mAnimator.setContentDescription(mDayPickerDescription+": "+dayString);
                Utils.tryAccessibilityAnnounce(mAnimator, mSelectDay);
                break;
            case YEAR_VIEW:
                pulseAnimator = Utils.getPulseAnimator(mYearView, 0.85f, 1.1f);
                if (mDelayAnimation) {
                    pulseAnimator.setStartDelay(ANIMATION_DELAY);
                    mDelayAnimation = false;
                }
                mYearPickerView.onDateChanged();
                if (mCurrentView != viewIndex) {
                    mMonthAndDayView.setSelected(false);
                    mYearView.setSelected(true);
                    mAnimator.setDisplayedChild(YEAR_VIEW);
                    mCurrentView = viewIndex;
                }
                pulseAnimator.start();

                CharSequence yearString = YEAR_FORMAT.format(millis);
                mAnimator.setContentDescription(mYearPickerDescription+": "+yearString);
                Utils.tryAccessibilityAnnounce(mAnimator, mSelectYear);
                break;
        }
    
public voidsetFirstDayOfWeek(int startOfWeek)

        if (startOfWeek < Calendar.SUNDAY || startOfWeek > Calendar.SATURDAY) {
            throw new IllegalArgumentException("Value must be between Calendar.SUNDAY and " +
                    "Calendar.SATURDAY");
        }
        mWeekStart = startOfWeek;
        if (mDayPickerView != null) {
            mDayPickerView.onChange();
        }
    
public voidsetMaxDate(java.util.Calendar calendar)
Sets the minimal date supported by this DatePicker. Dates after (but not including) the specified date will be disallowed from being selected.

param
calendar a Calendar object set to the year, month, day desired as the maxdate.

        mMaxDate = calendar;

        if (mDayPickerView != null) {
            mDayPickerView.onChange();
        }
    
public voidsetMinDate(java.util.Calendar calendar)
Sets the minimal date supported by this DatePicker. Dates before (but not including) the specified date will be disallowed from being selected.

param
calendar a Calendar object set to the year, month, day desired as the mindate.

        mMinDate = calendar;

        if (mDayPickerView != null) {
            mDayPickerView.onChange();
        }
    
public voidsetOnDateSetListener(com.android.datetimepicker.date.DatePickerDialog$OnDateSetListener listener)

        mCallBack = listener;
    
public voidsetYearRange(int startYear, int endYear)

        if (endYear <= startYear) {
            throw new IllegalArgumentException("Year end must be larger than year start");
        }
        mMinYear = startYear;
        mMaxYear = endYear;
        if (mDayPickerView != null) {
            mDayPickerView.onChange();
        }
    
public voidtryVibrate()

        mHapticFeedbackController.tryVibrate();
    
public voidunregisterOnDateChangedListener(com.android.datetimepicker.date.DatePickerDialog$OnDateChangedListener listener)

        mListeners.remove(listener);
    
private voidupdateDisplay(boolean announce)

        if (mDayOfWeekView != null) {
            mDayOfWeekView.setText(mCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG,
                    Locale.getDefault()).toUpperCase(Locale.getDefault()));
        }

        mSelectedMonthTextView.setText(mCalendar.getDisplayName(Calendar.MONTH, Calendar.SHORT,
                Locale.getDefault()).toUpperCase(Locale.getDefault()));
        mSelectedDayTextView.setText(DAY_FORMAT.format(mCalendar.getTime()));
        mYearView.setText(YEAR_FORMAT.format(mCalendar.getTime()));

        // Accessibility.
        long millis = mCalendar.getTimeInMillis();
        mAnimator.setDateMillis(millis);
        int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR;
        String monthAndDayText = DateUtils.formatDateTime(getActivity(), millis, flags);
        mMonthAndDayView.setContentDescription(monthAndDayText);

        if (announce) {
            flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;
            String fullDateText = DateUtils.formatDateTime(getActivity(), millis, flags);
            Utils.tryAccessibilityAnnounce(mAnimator, fullDateText);
        }
    
private voidupdatePickers()

        Iterator<OnDateChangedListener> iterator = mListeners.iterator();
        while (iterator.hasNext()) {
            iterator.next().onDateChanged();
        }