Free Android app covering all aspects of addiction, including prevention and treatment

A DatePicker tutorial

  • Written by  Clive

Pick your date

Android DatePicker icon

Date Pickers let you pick a date that is correctly formatted and adjusted to the user’s locale.

It’s best to show your DatePicker in a Dialog Fragment which has been around since API Level 11.

We’ll show you how to use a DatePicker in a Dialog Fragment on device’s running API Level 4 and above.

We use the support library’s DialogFragment. You can use the platform version of DialogFragment if you’re developing for API Level 11 and higher.

Our Tutorial app

We display a button when the app starts. Pressing the button displays a DatePicker.

Android DatePicker screenshot1

The DatePicker displays in a DialogFragment

The user selects a date and presses the Set button which displays the selected date in a Toast message.

Android DatePicker screenshot2

The selected date is displayed in a Toast message

The Fragment: Creating the Dialog Fragment

We’re using the support library’s version of a DialogFragment so our app will run on devices running API Level 4 and up.

Android DatePicker Support library DialogFragment 

Make sure that you import the support library’s DialogFragment class

The DialogFragment is a fragment that displays a floating dialog window on top of the activity’s window. It also manages the dialog’s lifecycle for you.

We create a DialogFragment class, MyDatePickerFragment to display our DatePicker. We also implement the OnDateSetListener interface to listen for when the user sets the date.

Android DatePicker Support library DialogFragment OnDateSetListener

Implement the OnDateSetListener interface to get the date set by the user

The Dialog: Creating a DatePickerDialog

Use the DialogFragment to display the DatePickerDialog.

We create a DatePickerDialog, setting its initial values to the current day, month and year which we get from a Calendar object.

Android DatePicker Support library DialogFragment calendar

Get the current day, month and year from the Calendar object

Note the following:

  • calendar – our Calendar object representing a specific instance in time
  • myDatePicker – a dialog containing a DatePicker. We create the dialog by passing the DatePickerDialog constructor 5 parameters:
    • the context in which the dialog should run
    • the callback which is used to get notified when the user sets the date
    • year – the initial year setting for the DatePicker
    • month - the initial month setting for the DatePicker
    • day - the initial day setting for the DatePicker

The Listener Interface: Listen for when the user sets the date

We use the OnDateSetListener interface’s onDateSet() method to get the date selected by the user.

Android DatePicker Support library DialogFragment onDateSet()

Use the onDateSet() method to get the user selected date

Note the following:

  • suffix – we use a few arrays and array lists to determine the day’s suffix (e.g. 1st, 2nd, 3rd, 4th) so that for example, if day equals 1 then the suffix will be st, 2 will be nd, etc.
  • we display the selected date in a Toast message
  • monthsArray[monthOfYear] – we use a String array of months, and the selected month’s value as the array’s index,  to get the matching month. Note that the array is indexed from 0 and that the selected month’s returned value is also indexed from 0. So selecting January returns 0 which matches the first item in our array

Showing the Date Picker

The Activity

The apps only activity, MyActivity activity must extend the FragmentActivity class. Also, remember we’re using the support library so you must make sure that you import the correct classes:

Android DatePicker Support library DialogFragment activitySupportClasses

Make sure to import the support library classes

Running the app displays a Button. Pressing the button executes the onClick method declared in the layout, doDatePicker().

Android DatePicker Support library DialogFragment doDatePicker

Use the DialogFragment’s show() method to display the DatePicker in the DialogFragment

Note the following:

To show the Date Picker, we first create an instance of our DialogFragment, MyDatePickerFragment. Then we call show() to show the fragment.

  • show – a convenience method that:
    • creates a fragment transaction
    • adds the fragment to the transaction (with a tag)
    • commits the transaction
  • getSupportFragmentManager – returns a fragment manager which enables us to interact with the fragment
  • datePicker – the tag name for the fragment which can be used to retrieve the fragment later

Have you seen our Fragment tutorials?

DatePicker Libraries

Need to customise the standard Android DatePicker?

Check out these DatePicker libraries. They may have the style and features that you’re looking for!

Derek Brameyer’s android-betterpickers DialogFragments library.

Flavien Laurent’s datetimepicker library.

Jake Wharton’s android-times-square library

I hope that you have found this tutorial helpful.

This project was created using Android Studio.

You can download the project files here Download icon

Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files.