Android date time picker example

Android-date-time-picker-example-featured.png

Pickers
In this document

See also

Fragments

Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs. Each picker provides controls for selecting each part of the time (hour, minute, AM/PM) or date (month, day, year). Using these pickers helps ensure that your users can pick a time or date that is valid, formatted correctly, and adjusted to the user’s locale.

We recommend that you use DialogFragment to host each time or date picker. The DialogFragment manages the dialog lifecycle for you and allows you to display the pickers in different layout configurations, such as in a basic dialog on handsets or as an embedded part of the layout on large screens.

Although DialogFragment was first added to the platform in Android 3.0 (API level 11), if your app supports versions of Android older than 3.0—even as low as Android 1.6—you can use the DialogFragment class that’s available in the support library for backward compatibility.

Note: The code samples below show how to create dialogs for a time picker and date picker using the support library APIs for DialogFragment. If your app’s minSdkVersion is 11 or higher, you can instead use the platform version of DialogFragment.
Creating a Time Picker

To display a TimePickerDialog using DialogFragment, you need to define a fragment class that extends DialogFragment and return a TimePickerDialog from the fragment’s onCreateDialog() method.

Note: If your app supports versions of Android older than 3.0, be sure you’ve set up your Android project with the support library as described in Setting Up a Project to Use a Library.
Extending DialogFragment for a time picker

To define a DialogFragment for a TimePickerDialog, you must:

Define the onCreateDialog() method to return an instance of TimePickerDialog
Implement the TimePickerDialog.OnTimeSetListener interface to receive a callback when the user sets the time.

Here’s an example:

public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}

See the TimePickerDialog class for information about the constructor arguments.

Now all you need is an event that adds an instance of this fragment to your activity.
Showing the time picker

Once you’ve defined a DialogFragment like the one shown above, you can display the time picker by creating an instance of the DialogFragment and calling show().

For example, here’s a button that, when clicked, calls a method to show the dialog:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>