All CategoriesAndroid App Development

Introduction to Android Time Picker Dialog with Example

Introduction
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.

<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

2) Add text views and button to display the time. Initialize TextViews, Button and some variables in MainActivity.java 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(){..}

setCurrentTimeOnView();
addListenerOnButton();

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

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

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

public void addListenerOnButton() {
btnChangeTime = (Button) findViewById(R.id.btnChangeTime);
btnChangeTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
}

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
timePicker1.setCurrentHour(hour);
timePicker1.setCurrentMinute(minute);
}
};

Output:

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:
https://github.com/hiteshbpatel/Android_Projects/tree/master/MyAndroidApp
Android Programming
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 MainActivity.java
1) Add below code in MyAndroidActivity.java

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 MainActivity.java 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");
mTimePickerField.setAccessible(true);
TimePicker mTimePicker = (TimePicker) mTimePickerField.get(this);
mTimePicker.setOnTimeChangedListener(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());
setTitle(title);
}

Output:
Set Maximum Time in Time Picker
Download Project Link:
https://github.com/hiteshbpatel/Android_Projects/tree/master/TimePickerDemo1
Keep visiting our site Acadgild for more updates on Andriod and other technologies.
Android Programming

Leave a Reply

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

Related Articles

Close