Android APIs
public class

DatePicker

extends FrameLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.DatePicker

Class Overview

Provides a widget for selecting a date.

When the DatePicker_datePickerMode attribute is set to spinner, the date can be selected using year, month, and day spinners or a CalendarView. The set of spinners and the calendar view are automatically synchronized. The client can customize whether only the spinners, or only the calendar view, or both to be displayed.

When the DatePicker_datePickerMode attribute is set to calendar, the month and day can be selected using a calendar-style view while the year can be selected separately using a list.

See the Pickers guide.

For a dialog using this view, see DatePickerDialog.

Summary

Nested Classes
interface DatePicker.OnDateChangedListener The callback used to indicate the user changes\d the date. 
XML Attributes
Attribute Name Related Method Description
android:calendarTextColor The text color list of the calendar. 
android:calendarViewShown Whether the calendar view is shown. 
android:datePickerMode Defines the look of the widget. 
android:dayOfWeekBackground The background color for the date selector 's day of week. 
android:dayOfWeekTextAppearance The text color for the date selector's day of week. 
android:endYear The last year (inclusive), for example "2010". 
android:firstDayOfWeek setFirstDayOfWeek(int) The first day of week according to Calendar
android:headerBackground The background for the date selector. 
android:headerDayOfMonthTextAppearance The day of month's text appearance in the date selector. 
android:headerMonthTextAppearance The month's text appearance in the date selector. 
android:headerYearTextAppearance The year's text appearance in the date selector. 
android:maxDate The maximal date shown by this calendar view in mm/dd/yyyy format. 
android:minDate The minimal date shown by this calendar view in mm/dd/yyyy format. 
android:spinnersShown Whether the spinners are shown. 
android:startYear The first year (inclusive), for example "1940". 
android:yearListItemTextAppearance The list year's text appearance in the list. 
android:yearListSelectorColor The list year's selected circle color in the list. 
[Expand]
Inherited XML Attributes
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
DatePicker(Context context)
DatePicker(Context context, AttributeSet attrs)
DatePicker(Context context, AttributeSet attrs, int defStyleAttr)
DatePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Public Methods
boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event)
Dispatches an AccessibilityEvent to the View first and then to its children for adding their text content to the event.
CalendarView getCalendarView()
Gets the CalendarView.
boolean getCalendarViewShown()
Gets whether the CalendarView is shown.
int getDayOfMonth()
int getFirstDayOfWeek()
Gets the first day of week.
long getMaxDate()
Gets the maximal date supported by this DatePicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
long getMinDate()
Gets the minimal date supported by this DatePicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
int getMonth()
boolean getSpinnersShown()
Gets whether the spinners are shown.
int getYear()
void init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
Initialize the state.
boolean isEnabled()
Returns the enabled status for this view.
void onInitializeAccessibilityEvent(AccessibilityEvent event)
Initializes an AccessibilityEvent with information about this View which is the event source.
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
Initializes an AccessibilityNodeInfo with information about this view.
void onPopulateAccessibilityEvent(AccessibilityEvent event)
Called from dispatchPopulateAccessibilityEvent(AccessibilityEvent) giving a chance to this View to populate the accessibility event with its text content.
void setCalendarViewShown(boolean shown)
Sets whether the CalendarView is shown.
void setEnabled(boolean enabled)
Set the enabled state of this view.
void setFirstDayOfWeek(int firstDayOfWeek)
Sets the first day of week.
void setMaxDate(long maxDate)
Sets the maximal date supported by this DatePicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
void setMinDate(long minDate)
Sets the minimal date supported by this NumberPicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
void setSpinnersShown(boolean shown)
Sets whether the spinners are shown.
void updateDate(int year, int month, int dayOfMonth)
Update the current date.
Protected Methods
void dispatchRestoreInstanceState(SparseArray<Parcelable> container)
Called by restoreHierarchyState(android.util.SparseArray) to retrieve the state for this view and its children.
void onConfigurationChanged(Configuration newConfig)
Called when the current configuration of the resources being used by the application have changed.
void onRestoreInstanceState(Parcelable state)
Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState().
Parcelable onSaveInstanceState()
Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state.
[Expand]
Inherited Methods
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android:calendarTextColor

The text color list of the calendar.

Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol calendarTextColor.

Related Methods

android:calendarViewShown

Whether the calendar view is shown.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol calendarViewShown.

Related Methods

android:datePickerMode

Defines the look of the widget. Prior to the L release, the only choice was spinner. As of L, with the Material theme selected, the default layout is calendar, but this attribute can be used to force spinner to be used instead.

Must be one of the following constant values.

ConstantValueDescription
spinner1 Date picker with spinner controls to select the date.
calendar2 Date picker with calendar to select the date.

This corresponds to the global attribute resource symbol datePickerMode.

Related Methods

android:dayOfWeekBackground

The background color for the date selector 's day of week.

May be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This corresponds to the global attribute resource symbol dayOfWeekBackground.

Related Methods

android:dayOfWeekTextAppearance

The text color for the date selector's day of week.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol dayOfWeekTextAppearance.

Related Methods

android:endYear

The last year (inclusive), for example "2010".

Must be an integer value, such as "100".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol endYear.

Related Methods

android:firstDayOfWeek

The first day of week according to Calendar.

Must be an integer value, such as "100".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol firstDayOfWeek.

Related Methods

android:headerBackground

The background for the date selector.

May be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This corresponds to the global attribute resource symbol headerBackground.

Related Methods

android:headerDayOfMonthTextAppearance

The day of month's text appearance in the date selector.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol headerDayOfMonthTextAppearance.

Related Methods

android:headerMonthTextAppearance

The month's text appearance in the date selector.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol headerMonthTextAppearance.

Related Methods

android:headerYearTextAppearance

The year's text appearance in the date selector.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol headerYearTextAppearance.

Related Methods

android:maxDate

The maximal date shown by this calendar view in mm/dd/yyyy format.

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol maxDate.

Related Methods

android:minDate

The minimal date shown by this calendar view in mm/dd/yyyy format.

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol minDate.

Related Methods

android:spinnersShown

Whether the spinners are shown.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol spinnersShown.

Related Methods

android:startYear

The first year (inclusive), for example "1940".

Must be an integer value, such as "100".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol startYear.

Related Methods

android:yearListItemTextAppearance

The list year's text appearance in the list.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol yearListItemTextAppearance.

Related Methods

android:yearListSelectorColor

The list year's selected circle color in the list.

Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol yearListSelectorColor.

Related Methods

Public Constructors

public DatePicker (Context context)

Added in API level 1

public DatePicker (Context context, AttributeSet attrs)

Added in API level 1

public DatePicker (Context context, AttributeSet attrs, int defStyleAttr)

Added in API level 1

public DatePicker (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Added in API level 21

Public Methods

public boolean dispatchPopulateAccessibilityEvent (AccessibilityEvent event)

Added in API level 4

Dispatches an AccessibilityEvent to the View first and then to its children for adding their text content to the event. Note that the event text is populated in a separate dispatch path since we add to the event not only the text of the source but also the text of all its descendants. A typical implementation will call onPopulateAccessibilityEvent(AccessibilityEvent) on the this view and then call the dispatchPopulateAccessibilityEvent(AccessibilityEvent) on each child. Override this method if custom population of the event text content is required.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its dispatchPopulateAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Accessibility events of certain types are not dispatched for populating the event text via this method. For details refer to AccessibilityEvent.

Parameters
event The event.
Returns
  • True if the event population was completed.

public CalendarView getCalendarView ()

Added in API level 12

Gets the CalendarView.

This method returns null when the DatePicker_datePickerMode attribute is set to calendar.

Returns
  • The calendar view.

public boolean getCalendarViewShown ()

Added in API level 11

Gets whether the CalendarView is shown.

Returns
  • True if the calendar view is shown.

public int getDayOfMonth ()

Added in API level 1

Returns
  • The selected day of month.

public int getFirstDayOfWeek ()

Added in API level 21

Gets the first day of week.

Related XML Attributes
Returns
  • The first day of the week conforming to the CalendarView APIs.

public long getMaxDate ()

Added in API level 11

Gets the maximal date supported by this DatePicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.

Note: The default maximal date is 12/31/2100.

Returns
  • The maximal supported date.

public long getMinDate ()

Added in API level 11

Gets the minimal date supported by this DatePicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.

Note: The default minimal date is 01/01/1900.

Returns
  • The minimal supported date.

public int getMonth ()

Added in API level 1

Returns
  • The selected month.

public boolean getSpinnersShown ()

Added in API level 11

Gets whether the spinners are shown.

Returns
  • True if the spinners are shown.

public int getYear ()

Added in API level 1

Returns
  • The selected year.

public void init (int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)

Added in API level 1

Initialize the state. If the provided values designate an inconsistent date the values are normalized before updating the spinners.

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

public boolean isEnabled ()

Added in API level 1

Returns the enabled status for this view. The interpretation of the enabled state varies by subclass.

Returns
  • True if this view is enabled, false otherwise.

public void onInitializeAccessibilityEvent (AccessibilityEvent event)

Added in API level 14

Initializes an AccessibilityEvent with information about this View which is the event source. In other words, the source of an accessibility event is the view whose state change triggered firing the event.

Example: Setting the password property of an event in addition to properties set by the super implementation:

 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
     super.onInitializeAccessibilityEvent(event);
     event.setPassword(true);
 }

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters
event The event to initialize.

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Added in API level 14

Initializes an AccessibilityNodeInfo with information about this view. The base implementation sets:

Subclasses should override this method, call the super implementation, and set additional attributes.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo) is responsible for handling this call.

Parameters
info The instance to initialize.

public void onPopulateAccessibilityEvent (AccessibilityEvent event)

Added in API level 14

Called from dispatchPopulateAccessibilityEvent(AccessibilityEvent) giving a chance to this View to populate the accessibility event with its text content. While this method is free to modify event attributes other than text content, doing so should normally be performed in onInitializeAccessibilityEvent(AccessibilityEvent).

Example: Adding formatted date string to an accessibility event in addition to the text added by the super implementation:

 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
     super.onPopulateAccessibilityEvent(event);
     final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
     String selectedDateUtterance = DateUtils.formatDateTime(mContext,
         mCurrentDate.getTimeInMillis(), flags);
     event.getText().add(selectedDateUtterance);
 }

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onPopulateAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters
event The accessibility event which to populate.

public void setCalendarViewShown (boolean shown)

Added in API level 11

Sets whether the CalendarView is shown.

Calling this method has no effect when the DatePicker_datePickerMode attribute is set to calendar.

Parameters
shown True if the calendar view is to be shown.

public void setEnabled (boolean enabled)

Added in API level 1

Set the enabled state of this view. The interpretation of the enabled state varies by subclass.

Parameters
enabled True if this view is enabled, false otherwise.

public void setFirstDayOfWeek (int firstDayOfWeek)

Added in API level 21

Sets the first day of week.

Related XML Attributes
Parameters
firstDayOfWeek The first day of the week conforming to the CalendarView APIs.

public void setMaxDate (long maxDate)

Added in API level 11

Sets the maximal date supported by this DatePicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.

Parameters
maxDate The maximal supported date.

public void setMinDate (long minDate)

Added in API level 11

Sets the minimal date supported by this NumberPicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.

Parameters
minDate The minimal supported date.

public void setSpinnersShown (boolean shown)

Added in API level 11

Sets whether the spinners are shown.

Parameters
shown True if the spinners are to be shown.

public void updateDate (int year, int month, int dayOfMonth)

Added in API level 1

Update the current date.

Parameters
year The year.
month The month which is starting from zero.
dayOfMonth The day of the month.

Protected Methods

protected void dispatchRestoreInstanceState (SparseArray<Parcelable> container)

Added in API level 1

Called by restoreHierarchyState(android.util.SparseArray) to retrieve the state for this view and its children. May be overridden to modify how restoring happens to a view's children; for example, some views may want to not store state for their children.

Parameters
container The SparseArray which holds previously saved state.

protected void onConfigurationChanged (Configuration newConfig)

Added in API level 8

Called when the current configuration of the resources being used by the application have changed. You can use this to decide when to reload resources that can changed based on orientation and other configuration characterstics. You only need to use this if you are not relying on the normal Activity mechanism of recreating the activity instance upon a configuration change.

Parameters
newConfig The new resource configuration.

protected void onRestoreInstanceState (Parcelable state)

Added in API level 1

Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState(). This function will never be called with a null state.

Parameters
state The frozen state that had previously been returned by onSaveInstanceState().

protected Parcelable onSaveInstanceState ()

Added in API level 1

Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state. This state should only contain information that is not persistent or can not be reconstructed later. For example, you will never store your current position on screen because that will be computed again when a new instance of the view is placed in its view hierarchy.

Some examples of things you may store here: the current cursor position in a text view (but usually not the text itself since that is stored in a content provider or other persistent storage), the currently selected item in a list view.

Returns
  • Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save. The default implementation returns null.