My application show a TimePickerDialog to set a time. I want that the timePickerDialog show the minutes with an interval of 5 minutes.
This works fine with this code:
private final int TIME_PICKER_INTERVAL=5;
private boolean mIgnoreEvent=false;
…
public TimePickerDialogs(Context arg0, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) {
super(arg0, callBack, hourOfDay, minute, is24HourView);
formato=Statics.formato;
}
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
//super.onTimeChanged(arg0, arg1, arg2);
if (mIgnoreEvent)
return;
if (minute%TIME_PICKER_INTERVAL!=0){
int minuteFloor=minute-(minute%TIME_PICKER_INTERVAL);
minute=minuteFloor + (minute==minuteFloor+1 ? TIME_PICKER_INTERVAL : 0);
if (minute==60)
minute=0;
mIgnoreEvent=true;
view.setCurrentMinute(minute);
mIgnoreEvent=false;
}
}
Although only minutes can be selected with an interval of five minutes, the timepickerdialog looks like:
do not know how the minutes also show the range of 5 minutes, as in this picture:
I have searched but can not find the solution.
Use the following the custom class called
CustomTimePickerDialog
, which I think solve your problem.Here is the demonstrating screenshot.
The answer above works really fine for older version of Android, but it didn't work for me in Android 7 Nougat, basically the date picker is changed completely to use the Clock mode... I kind of merge the code proposed here with https://gist.github.com/jeffdgr8/6bc5f990bf0c13a7334ce385d482af9f, then it set by default the mode of the TimePickerDialog to show the Spinner controls, and it runs the code to show only 15minuts intervals in my case.
The code is posted here https://gist.github.com/smaugho/14dae83f3284fa05455ee0a9e4f13099
}
There is this awesome library MaterialDateTimePicker that creates beautiful styled material timepickers.
You can use one of the following methods to define your interval:
Looking at the code for
TimePickerDialog
, it doesn't appear they give you a way to directly modify theTimePicker
before it gets shown to the user and you see the "wrong minutes" are you show in the picture. See TimePickerDialog.javaMy recommendation is to create your own TimePickerDialog, basing the design off the Android one. It might sound daunting at first, but
TimePicker
values to display for the minutes (as mentioned here), and alsoStruggled with 30 min interval more then hour. Might struggle more if wouldn't see the comment of @Lee. So just post a full code of dialog with 30 min interval: