Hello, Views >

Time Picker

To provide a widget for selecting a time, use the TimePicker widget, which allows the user to select the hour and minute in a familiar interface.

In this tutorial, you'll create a TimePickerDialog, which presents the time picker in a floating dialog box at the press of a button. When the time is set by the user, a TextView will update with the new date.

  1. Start a new project named HelloTimePicker.
  2. Open the res/layout/main.xml file and insert the following:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        <TextView android:id="@+id/timeDisplay"
        <Button android:id="@+id/pickTime"
            android:text="Change the time"/>

    This is a basic LinearLayout with a TextView that will display the time and a Button that will open the TimePickerDialog.

  3. Open HelloTimePicker.java and insert the following class members:
        private TextView mTimeDisplay;
        private Button mPickTime;
        private int mHour;
        private int mMinute;
        static final int TIME_DIALOG_ID = 0;

    This declares variables for the layout elements and time fields. The TIME_DIALOG_ID is a static integer that uniquely identifies the dialog.

  4. Now insert the following code for the onCreate() method:
        protected void onCreate(Bundle savedInstanceState) {
            // capture our View elements
            mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
            mPickTime = (Button) findViewById(R.id.pickTime);
            // add a click listener to the button
            mPickTime.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
            // get the current time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);
            // display the current date

    First, the content is set to the main.xml layout and then the TextView and Button are captured with findViewById(int). Then an View.OnClickListener is created for the Button, so that when clicked, it will call showDialog(int), passing the unique integer ID for the time picker dialog. Using showDialog(int) allows the Activity to manage the life-cycle of the dialog and will call the onCreateDialog(int) callback method to request the Dialog that should be displayed (which you'll define later). After the on-click listener is set, a new Calendar is created to get the current hour and minute. Finally, the private updateDisplay() method is called in order to fill the TextView with the current time.

  5. Add the updateDisplay() and pad() methods:
    // updates the time we display in the TextView
    private void updateDisplay() {
            new StringBuilder()
    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
            return "0" + String.valueOf(c);

    The updateDisplay() method uses the member fields for the time and inserts them in the mTimeDisplay TextView. The pad() method returns the appropriate string representation of the hour or minute—it will prefix a zero to the number if it's a single digit.

  6. Add a class member for a TimePickerDialog.OnTimeSetListener that will be called when the user sets a new time:
    // the callback received when the user "sets" the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;

    When the user is done setting the time (clicks the "Set" button), the onTimeSet() method is called and it updates the member fields with the new time and updates the layout's TextView.

  7. Add the onCreateDialog(int) callback method:
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, mHour, mMinute, false);
        return null;

    This is an Activity callback that is passed the identifier you passed to showDialog(int), in the Button's on-click listener. When the ID matches, this initializes the TimePickerDialog with the member variables initialized at the end of onCreate() and the TimePickerDialog.OnTimeSetListener created in the previous step.

  8. Run the application.

When you press the "Change the time" button, you should see the following:


  1. TimePicker
  2. TimePickerDialog.OnTimeSetListener
  3. Button
  4. TextView
  5. Calendar
↑ Go to top

← Back to Hello, Views