Using a combination of this from @Rizwan and this in another topic, I came up with a combined solution that allows arbitrary minute increments in TimePickerDialog . The main problem is that most of the functions are hidden by the android classes TimePickerDialog and TimePicker , and this is not like
- Extend
TimePickerDialog to facilitate access. - Use reflection to reach inside the display and access the required bits (see below).
- rename the minute "NumberPicker" to display our values
- reinstall
TimePicker to get and return values โโfrom NumberPicker in compliance with our custom increment. - lock onStop () so that it does not reset the value when closed.
A warning
The main problem with reaching inside the user interface is that elements are referenced by identifiers, which may change, and even the identifier name is not guaranteed forever. Having said that, it works, a stable solution, and is likely to work in the foreseeable future. In my opinion, an empty blocking block should warn that the user interface has changed and should return to default (in 1 minute increments).
Decision
private class DurationTimePickDialog extends TimePickerDialog { final OnTimeSetListener mCallback; TimePicker mTimePicker; final int increment; public DurationTimePickDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, int increment) { super(context, callBack, hourOfDay, minute/increment, is24HourView); this.mCallback = callBack; this.increment = increment; } @Override public void onClick(DialogInterface dialog, int which) { if (mCallback != null && mTimePicker!=null) { mTimePicker.clearFocus(); mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute()*increment); } } @Override protected void onStop() {
the constructor takes an increment value and saves some other references. Note that this excludes error checking and we would prefer 60%increment==0
onCreate uses the interface field name and reflection to locate the current location. Again, this eliminates error checking and should be "fault tolerant", i.e. revert to default behavior if something goes wrong.
onClick is overridden to return the correct minute value to the callback listener
onStop is redefined to prevent the (incorrect) index value from returning a second time the dialog closes. Go ahead, try it yourself.
Most of this comes from what you dig into the source TimePickerDialog.