---
title: "Time picker in Android Application"  
description: "In this article, I am going to explain how to create a TimePickerDialog, which presents the time picker in a floating dialog box at the press of a but"  
author: "Chris Anderson"  
published: 2011-10-26  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/793/time-picker-in-android-application  
category: "android"  
tags: ["android"]  
reading_time: 4 minutes  

---

# Time picker in Android Application

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 article, I am going to explain how to 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.

· Start a new project named TimePickerDemo.

· 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"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
<TextView
        android:id="@+id/timeDisplay"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="10dp"
     android:text="@string/hello"/>
<Button
        android:id="@+id/timePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Change the time"/>
</LinearLayout>
```

Open the activity file and add the following code as shown below:

```
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class TimePickerActivity extends Activity {

        private TextView mTimeDisplay;
        private Button mPickTime;
        private int mHour;
        private int mMinute;

        static final int TIME_DIALOG_ID=0;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         // capture our View elements
         mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
         mPickTime=(Button)findViewById(R.id.timePicker);

         // add a click listener to the button
         mPickTime.setOnClickListener(new View.OnClickListener() {

                      public void onClick(View v) {

                           showDialog(TIME_DIALOG_ID);
                     }
              });

         // 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
         updateDisplay();
    }

     // updates the time we display in the TextView
     private void updateDisplay()
    {
         mTimeDisplay.setText(new
                StringBuilder().append(pad(
               StringBuilder().append(pad(mHour)).append(":").append(pad(mMinute)));
    }

        private Object pad(int mMinute2) {

               if(mMinute2>=10)
                      return String.valueOf(mMinute2);
               else
                      return "0"+String.valueOf(mMinute2);
       }

        // the callback received when the user "sets" the time in the dialog
        private TimePickerDialog.OnTimeSetListener mtimeSetListener=new
                                             TimePickerDialog.OnTimeSetListener() {
                                            TimePickerDialog.OnTimeSetListener() {

               public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                      mHour=hourOfDay;
                      mMinute=minute;
                     updateDisplay();
              }
       };

        @Override
        protected Dialog onCreateDialog(int id)
       {
           switch(id)
          {
             case TIME_DIALOG_ID:
               return new TimePickerDialog(this,mtimeSetListener,mHour,mMinute,false);
          }
           return null;
       }
}
```

· Run the application.\
**Output will something like below:**

![Time picker in Android Application](https://www.mindstick.com/mindstickarticle/7deb797b-fafe-4dfe-9641-04f14eef3c8e/images/887f342a-6e5d-45f5-8d15-b6c3267fca0d.png)

When you click on the button (Change the time), a time picker dialog will open, you can set the time from the time picker dialog.

![Time picker in Android Application](https://www.mindstick.com/mindstickarticle/7deb797b-fafe-4dfe-9641-04f14eef3c8e/images/71959cca-361c-42e3-8448-d188b322249a.png)

After setting the time, a new time will display in the TextView.

---

Original Source: https://www.mindstick.com/articles/793/time-picker-in-android-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
