'Android - How to limit date time picker to only every X minutes?
I am storing date and time but I need to have it limited to only be allowed to pick intervals of 30 minutes when the time picker is displayed so I can't be able for example to pick time 06:11 .
Here is my code that I have.
private void showDateTimeDialog(final EditText date_time_in) {
final Calendar calendar=Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
TimePickerDialog.OnTimeSetListener timeSetListener=new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
//format of the calender
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yy-MM-dd HH:mm");
//add the booking in Firebase
date_time_in.setText(simpleDateFormat.format(calendar.getTime()));
Bookings booking = new Bookings(year,month+1,dayOfMonth,hourOfDay,minute);
rootDatabaseRef.push().setValue(booking);
Toast.makeText(BookingActivity.this, "Appointment successful ", Toast.LENGTH_SHORT).show();
date_time_in.setText(getString(R.string.select_a_date_and_timeText));
}
};
new TimePickerDialog(BookingActivity.this,timeSetListener,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),false).show();
}
};
new DatePickerDialog(BookingActivity.this,dateSetListener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
What changes do I need to make to limit to 30 minute intervals ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
