public class

DatePicker

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

Class Overview

This class is a widget for selecting a date. The date can be selected by a 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. Also the minimal and maximal date from which dates to be selected can be customized.

See the Date Picker tutorial.

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:calendarViewShown Whether the calendar view is shown. 
android:endYear The last year (inclusive), for example "2010". 
android:maxDate The minimal 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". 
[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 defStyle)
Public Methods
boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event)
Dispatches an AccessibilityEvent to the View children to be populated.
boolean getCalendarViewShown()
Gets whether the CalendarView is shown.
int getDayOfMonth()
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 setCalendarViewShown(boolean shown)
Sets whether the CalendarView is shown.
void setEnabled(boolean enabled)
Set the enabled state of this view.
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)
Updates the current date.
Protected Methods
void dispatchRestoreInstanceState(SparseArray<Parcelable> container)
Called by restoreHierarchyState(SparseArray) to retrieve the state for this view and its children.
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.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.ViewManager
From interface android.view.ViewParent
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android:calendarViewShown

Since: API Level

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:endYear

Since: API Level

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:maxDate

Since: API Level

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 maxDate.

Related Methods

android:minDate

Since: API Level

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

Since: API Level

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

Since: API Level

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

Public Constructors

public DatePicker (Context context)

Since: API Level 1

public DatePicker (Context context, AttributeSet attrs)

Since: API Level 1

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

Since: API Level 1

Public Methods

public boolean dispatchPopulateAccessibilityEvent (AccessibilityEvent event)

Since: API Level 4

Dispatches an AccessibilityEvent to the View children to be populated.

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

public boolean getCalendarViewShown ()

Since: API Level 11

Gets whether the CalendarView is shown.

Returns
  • True if the calendar view is shown.

public int getDayOfMonth ()

Since: API Level 1

Returns
  • The selected day of month.

public long getMaxDate ()

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

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

Since: API Level 1

Returns
  • The selected month.

public boolean getSpinnersShown ()

Since: API Level 11

Gets whether the spinners are shown.

Returns
  • True if the spinners are shown.

public int getYear ()

Since: API Level 1

Returns
  • The selected year.

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

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

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

Since: API Level 11

Sets whether the CalendarView is shown.

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

public void setEnabled (boolean enabled)

Since: 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 setMaxDate (long maxDate)

Since: 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)

Since: 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)

Since: 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)

Since: API Level 1

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

Since: API Level 1

Called by restoreHierarchyState(SparseArray) to retrieve the state for this view and its children. May be overridden to modify how restoreing 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 onRestoreInstanceState (Parcelable state)

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

Since: 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.