Skip to content

Most visited

Recently visited

navigation

DatePicker

public class DatePicker
extends FrameLayout

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


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 changed the date. 

XML attributes

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 header's day of week. 
android:dayOfWeekTextAppearance The text color for the header's day of week. 
android:endYear The last year (inclusive), for example "2010". 
android:firstDayOfWeek The first day of week according to Calendar
android:headerBackground The background for the selected date header. 
android:headerDayOfMonthTextAppearance The text appearance for the day of month (ex. 
android:headerMonthTextAppearance The text appearance for the month (ex. 
android:headerYearTextAppearance The text appearance for the year (ex. 
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. 

Inherited XML attributes

From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View

Inherited constants

From class android.view.ViewGroup
From class android.view.View

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

void autofill(AutofillValue value)

Automatically fills the content of this view with the value.

void dispatchProvideAutofillStructure(ViewStructure structure, int flags)

Dispatches creation of a ViewStructures for autofill purposes down the hierarchy, when an Assist structure is being created as part of an autofill request.

This implementation adds in all child views of the view group, in addition to calling the default View implementation.

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

int getAutofillType()

Describes the autofill type of this view, so an AutofillService can create the proper AutofillValue when autofilling the view.

AutofillValue getAutofillValue()

Gets the View's current autofill value.

CalendarView getCalendarView()

This method was deprecated in API level 24. Not supported by Material-style calendar mode

boolean getCalendarViewShown()

This method was deprecated in API level 24. Not supported by Material-style calendar mode

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()

This method was deprecated in API level 24. Not supported by Material-style calendar mode

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 setCalendarViewShown(boolean shown)

This method was deprecated in API level 24. Not supported by Material-style calendar mode

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 setOnDateChangedListener(DatePicker.OnDateChangedListener onDateChangedListener)

Set the callback that indicates the date has been adjusted by the user.

void setSpinnersShown(boolean shown)

This method was deprecated in API level 24. Not supported by Material-style calendar mode

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.

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.

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

android:calendarViewShown

Whether the calendar view is shown. Only valid for "spinner" mode.

May be a boolean value, such as "true" or "false".

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
calendar2Date picker with calendar to select the date.
spinner1Date picker with spinner controls to select the date.

android:dayOfWeekBackground

The background color for the header's day of week.

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

android:dayOfWeekTextAppearance

The text color for the header's day of week.

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

android:endYear

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

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

android:firstDayOfWeek

The first day of week according to Calendar.

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

Related methods:

android:headerBackground

The background for the selected date header.

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

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

android:headerDayOfMonthTextAppearance

The text appearance for the day of month (ex. 28) in the selected date header.

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

android:headerMonthTextAppearance

The text appearance for the month (ex. May) in the selected date header.

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

android:headerYearTextAppearance

The text appearance for the year (ex. 2014) in the selected date header.

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

android:maxDate

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

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

android:minDate

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

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

android:spinnersShown

Whether the spinners are shown. Only valid for "spinner" mode.

May be a boolean value, such as "true" or "false".

android:startYear

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

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

android:yearListItemTextAppearance

The list year's text appearance in the list.

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

android:yearListSelectorColor

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

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

Public constructors

DatePicker

added in API level 1
DatePicker (Context context)

Parameters
context Context

DatePicker

added in API level 1
DatePicker (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

DatePicker

added in API level 1
DatePicker (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

DatePicker

added in API level 21
DatePicker (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

autofill

added in API level 26
void autofill (AutofillValue value)

Automatically fills the content of this view with the value.

Views support the Autofill Framework mainly by:

  • Providing the metadata defining what the view means and how it can be autofilled.
  • Implementing the methods that autofill the view.

onProvideAutofillStructure(ViewStructure, int) is responsible for the former, this method is responsible for latter.

This method does nothing by default, but when overridden it typically:

  1. Checks if the provided value matches the expected type (which is defined by getAutofillType()).
  2. Checks if the view is editable - if it isn't, it should return right away.
  3. Call the proper getter method on AutofillValue to fetch the actual value.
  4. Pass the actual value to the equivalent setter in the view.

For example, a text-field view could implement the method this way:

 @Override
 public void autofill(AutofillValue value) {
   if (!value.isText() || !this.isEditable()) {
      return;
   }
   CharSequence text = value.getTextValue();
   if (text != null) {
     this.setText(text);
   }
 }
 

If the value is updated asynchronously, the next call to notifyValueChanged(View) must happen after the value was changed to the autofilled value. If not, the view will not be considered autofilled.

Parameters
value AutofillValue: value to be autofilled.

dispatchProvideAutofillStructure

added in API level 26
void dispatchProvideAutofillStructure (ViewStructure structure, 
                int flags)

Dispatches creation of a ViewStructures for autofill purposes down the hierarchy, when an Assist structure is being created as part of an autofill request.

The default implementation does the following:

Typically, this method should only be overridden by subclasses that provide a view hierarchy (such as ViewGroup) - other classes should override onProvideAutofillStructure(ViewStructure, int) or onProvideAutofillVirtualStructure(ViewStructure, int) instead.

When overridden, it must:

  • Either call super.dispatchProvideAutofillStructure(structure, flags) or explicitly set the AutofillId in the structure (for example, by calling structure.setAutofillId(getAutofillId())).
  • Decide how to handle the AUTOFILL_FLAG_INCLUDE_NOT_IMPORTANT_VIEWS flag - when set, all views in the structure should be considered important for autofill, regardless of what isImportantForAutofill() returns. We encourage you to respect this flag to provide a better user experience - this flag is typically used when an user explicitly requested autofill. If the flag is not set, then only views marked as important for autofill should be included in the structure - skipping non-important views optimizes the overall autofill performance.

This implementation adds in all child views of the view group, in addition to calling the default View implementation.

Parameters
structure ViewStructure: fill in with structured view data for autofill purposes.

flags int: optional flags.

getAccessibilityClassName

added in API level 23
CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

Returns
CharSequence

getAutofillType

added in API level 26
int getAutofillType ()

Describes the autofill type of this view, so an AutofillService can create the proper AutofillValue when autofilling the view.

By default returns AUTOFILL_TYPE_NONE, but views should override it to properly support the Autofill Framework.

Returns
int either AUTOFILL_TYPE_NONE, AUTOFILL_TYPE_TEXT, AUTOFILL_TYPE_LIST, AUTOFILL_TYPE_DATE, or AUTOFILL_TYPE_TOGGLE.

getAutofillValue

added in API level 26
AutofillValue getAutofillValue ()

Gets the View's current autofill value.

By default returns null, but views should override it to properly support the Autofill Framework.

Returns
AutofillValue

getCalendarView

added in API level 12
CalendarView getCalendarView ()

This method was deprecated in API level 24.
Not supported by Material-style calendar mode

Returns the CalendarView used by this picker.

Note: This method throws an UnsupportedOperationException when the DatePicker_datePickerMode attribute is set to calendar.

Returns
CalendarView the calendar view

Throws
UnsupportedOperationException if called when the picker is displayed in calendar mode

See also:

getCalendarViewShown

added in API level 11
boolean getCalendarViewShown ()

This method was deprecated in API level 24.
Not supported by Material-style calendar mode

Returns whether the CalendarView is shown.

Note: This method returns false when the DatePicker_datePickerMode attribute is set to calendar.

Returns
boolean true if the calendar view is shown

See also:

getDayOfMonth

added in API level 1
int getDayOfMonth ()

Returns
int The selected day of month.

getFirstDayOfWeek

added in API level 21
int getFirstDayOfWeek ()

Gets the first day of week.

Related XML Attributes:

Returns
int The first day of the week conforming to the CalendarView APIs.

See also:

getMaxDate

added in API level 11
long getMaxDate ()

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
long The maximal supported date.

getMinDate

added in API level 11
long getMinDate ()

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
long The minimal supported date.

getMonth

added in API level 1
int getMonth ()

Returns
int The selected month.

getSpinnersShown

added in API level 11
boolean getSpinnersShown ()

This method was deprecated in API level 24.
Not supported by Material-style calendar mode

Returns whether the spinners are shown.

Note: his method returns false when the DatePicker_datePickerMode attribute is set to calendar.

Returns
boolean true if the spinners are shown

getYear

added in API level 1
int getYear ()

Returns
int The selected year.

init

added in API level 1
void init (int year, 
                int monthOfYear, 
                int dayOfMonth, 
                DatePicker.OnDateChangedListener onDateChangedListener)

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

Parameters
year int: The initial year.

monthOfYear int: The initial month starting from zero.

dayOfMonth int: The initial day of the month.

onDateChangedListener DatePicker.OnDateChangedListener: How user is notified date is changed by user, can be null.

isEnabled

added in API level 1
boolean isEnabled ()

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

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

setCalendarViewShown

added in API level 11
void setCalendarViewShown (boolean shown)

This method was deprecated in API level 24.
Not supported by Material-style calendar mode

Sets whether the CalendarView is shown.

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

Parameters
shown boolean: true to show the calendar view, false to hide it

setEnabled

added in API level 1
void setEnabled (boolean enabled)

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

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

setFirstDayOfWeek

added in API level 21
void setFirstDayOfWeek (int firstDayOfWeek)

Sets the first day of week.

Related XML Attributes:

Parameters
firstDayOfWeek int: The first day of the week conforming to the CalendarView APIs.

See also:

setMaxDate

added in API level 11
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.

Parameters
maxDate long: The maximal supported date.

setMinDate

added in API level 11
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.

Parameters
minDate long: The minimal supported date.

setOnDateChangedListener

added in API level 26
void setOnDateChangedListener (DatePicker.OnDateChangedListener onDateChangedListener)

Set the callback that indicates the date has been adjusted by the user.

Parameters
onDateChangedListener DatePicker.OnDateChangedListener: How user is notified date is changed by user, can be null.

setSpinnersShown

added in API level 11
void setSpinnersShown (boolean shown)

This method was deprecated in API level 24.
Not supported by Material-style calendar mode

Sets whether the spinners are shown.

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

Parameters
shown boolean: true to show the spinners, false to hide them

updateDate

added in API level 1
void updateDate (int year, 
                int month, 
                int dayOfMonth)

Update the current date.

Parameters
year int: The year.

month int: The month which is starting from zero.

dayOfMonth int: The day of the month.

Protected methods

dispatchRestoreInstanceState

added in API level 1
void dispatchRestoreInstanceState (SparseArray<Parcelable> container)

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 SparseArray: The SparseArray which holds previously saved state.

onConfigurationChanged

added in API level 8
void onConfigurationChanged (Configuration newConfig)

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 characteristics. 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 Configuration: The new resource configuration.

onRestoreInstanceState

added in API level 1
void onRestoreInstanceState (Parcelable state)

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 Parcelable: The frozen state that had previously been returned by onSaveInstanceState().

onSaveInstanceState

added in API level 1
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. 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
Parcelable Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.