3

How do I set an Android TimePickerDialog to use 15 minute intervals?

I need to create the picker programmatically as shown below so solutions involving a timepicker in XML (such as here: Android - TimePicker minutes to 15) will not work.

new TimePickerDialog(getActivity(), t, hour,minute, false).show();

The problem is that there appears to be no way to pass in a new function for TimePicker .setOnTimeChangedListener() when the time picker is created.

Is it possible to use the new TimePIckerDialog constructor and use 15 minute intervals?

Community
  • 1
  • 1
joe
  • 16,988
  • 36
  • 94
  • 131

3 Answers3

2

Override the onTimeChanged method

TimePickerDialog timePickerDialog = new TimePickerDialog(this, listener, 1, 1, true) {
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            ...
        }
};

timePickerDialog.show();
Arkadiusz Cieśliński
  • 5,307
  • 3
  • 23
  • 19
Georgy Gobozov
  • 13,633
  • 8
  • 72
  • 78
2

all relative answer need you to set an OnTimeChangedListener. My resolution is that you extends android TimePicker,and modify the constructor of it:

// minute
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
mMinuteSpinner.setMinValue(0);
mMinuteSpinner.setMaxValue(3);
mMinuteSpinner.setDisplayedValues(new String[]{"0", "15", "30", "45"});
mMinuteSpinner.setOnLongPressUpdateInterval(100);
mMinuteSpinner.setFormatter(NumberPicker.getTwoDigitFormatter());

so you can have the interval you want.

01.sunlit
  • 305
  • 1
  • 2
  • 8
0

Gathering partial solutions from this and other posts, I believe the solution here ticks all of the boxes for displaying a TimePickerDialog with a custom minute increment displayed in the NumberPicker.

Community
  • 1
  • 1
David O'Meara
  • 2,983
  • 25
  • 38