Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set TimePickerDialog to use 15 minute intervals

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?

like image 624
joe Avatar asked Oct 20 '25 13:10

joe


2 Answers

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.

like image 95
01.sunlit Avatar answered Oct 23 '25 02:10

01.sunlit


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();
like image 24
Georgy Gobozov Avatar answered Oct 23 '25 03:10

Georgy Gobozov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!