Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • Introduction to Android Time Picker Dialog with Example

Introduction to Android Time Picker Dialog with Example

 July 8  | 0 Comments

Introduction to Android Time Picker:

Android Time Picker allows you to select the time in 24 hours mode or in AM/PM mode. The time displayed will be in hours, minutes, AM/PM format.

You can use this Time Picker to set alarm.

In this example, we will see how to use time picker to set the time. We will also set the hour, minute and time format in time picker.
1) First step is to add timepicker in activity_main.xml.

Get Skilled in Android Development
android:layout_height="wrap_content" />

2) Add text views and button to display the time. Initialize TextViews, Button and some variables in like:

private TextView tvDisplayTime;
private TimePicker timePicker1;
private Button btnChangeTime;
private int hour;
private int minute;
static final int TIME_DIALOG_ID = 999;

3) Add some methods in onCreate(){..}


To display current time add below code lines in setCurrentTimeOnView(){..}

// display current time
public void setCurrentTimeOnView() {
tvDisplayTime = (TextView) findViewById(;
timePicker1 = (TimePicker) findViewById(;
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
// set current time into textview
new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
// set current time into timepicker

4) Add below code in addListenerOnButton(){..}

public void addListenerOnButton() {
btnChangeTime = (Button) findViewById(;
btnChangeTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

Create a TimePickerDialog.onTimeSetListener(){..}

private TimePickerDialog.OnTimeSetListener timePickerListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
// set current time into textview
tvDisplayTime.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
// set current time into timepicker


Note : Click on the Change Time Button. It will prompt a time picker component in a dialog and you can change time from there:

Download Project Link:

How to set Minimum and Maximum time in Time Picker:
Now we will how to set the minimum and maximum time in Time Picker. For this we have to add code in
1) Add below code in

private int minHour = -1;
private int minMinute = -1;
private int maxHour = 25;
private int maxMinute = 25;
private int currentHour = 0;
private int currentMinute = 0;
private Calendar calendar = Calendar.getInstance();
private DateFormat dateFormat;

2) Create a constructor of your class and add below code in it.

public MainActivity(Context context, OnTimeSetListener listener, int hourOfDay, int minute, boolean is24HourView) {
super(context, listener, hourOfDay, minute, is24HourView);
currentHour = hourOfDay;
currentMinute = minute;
dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
try {
Class<?> superclass = getClass().getSuperclass();
Field mTimePickerField = superclass.getDeclaredField("mTimePicker");
TimePicker mTimePicker = (TimePicker) mTimePickerField.get(this);
} catch (NoSuchFieldException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {

3) Add two methods setMin(){..} and setMax(){..} to set the minimum or maximum time :

public void setMin(int hour, int minute) {
minHour = hour;
minMinute = minute;
public void setMax(int hour, int minute) {
maxHour = hour;
maxMinute = minute;

4) Add a method onTimeChanged(){..} and updateTitleDialog(){..}

public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
boolean validTime = true;
if (hourOfDay < minHour || (hourOfDay == minHour && minute < minMinute)){
validTime = false;
if (hourOfDay > maxHour || (hourOfDay == maxHour && minute > maxMinute)){
validTime = false;
if (validTime) {
currentHour = hourOfDay;
currentMinute = minute;
updateTime(currentHour, currentMinute);
updateDialogTitle(view, currentHour, currentMinute);
private void updateDialogTitle(TimePicker timePicker, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
String title = dateFormat.format(calendar.getTime());