After searching the net, I didn't find the simplest solution.
So I decided to share:
You can use reflections!
The following code use intervals of 10 minutes, but you can choose the minutes you want to use in the DISPLAYED_MINS array.
private final static String[] DISPLAYED_MINS = { "0", "10", "20", "30", "40", "50" };
private NumberPicker getMinuteSpinner(TimePicker t)
{
try
{
Field f = t.getClass().getDeclaredField("mMinuteSpinner"); // NoSuchFieldException
f.setAccessible(true);
return (NumberPicker) f.get(t); // IllegalAccessException
}
catch (Exception e)
{
Log.e("timepicker","field name has been changed, check grepcode");
return null;
}
}
when you create your timepicker:
final TimePicker dpStartDate = (TimePicker) view.findViewById(R.id.dpStartDate);
NumberPicker startMinSpiner = getMinuteSpinner(dpStartDate);
if (null != startMinSpiner)
{
startMinSpiner.setMinValue(0);
startMinSpiner.setMaxValue(DISPLAYED_MINS.length - 1);
startMinSpiner.setDisplayedValues(DISPLAYED_MINS);
}
Just don't forget that getCurrentMinute() return the selected minute in the DISPLAY_MINS array.
in order to get the minute:
int theTime = 0;
for (String number: DISPLAYED_MINS) {
int theNumber = Integer.parseInt(number);
if (theNumber % 5 != 0) {
theNumber = ((int)(theNumber / 5)) * 5;
}
if (theNumber == dpStartDate.getCurrentMinute()) {
theTime = theNumber;
break;
}
}