3

Android Timepickerdialog is changing hours illogically when I'm scrolling minutes. I have set the minute interval to 30 minutes in my timepickerdialog so the values for minutes are 00 and 30. For example when I set the hours to 00 and change minutes to from 00 to 30 the hours are decreased one hour so the time is now 23:30 and what I'm suppose to have is 00:30. I realized that this doesn't happen on Samsung devices but happens on every other manufacturers devices. Is there any way to disable hours change when scrolling the minutes?

Here is my code

public class CustomTimePickerDialog extends TimePickerDialog {

    private int TIME_PICKER_INTERVAL = 30;
    private TimePicker mTimePicker;
    private final OnTimeSetListener mTimeSetListener;
    public int interval1 = 1;

    public CustomTimePickerDialog(Context context, OnTimeSetListener listener,
                                  int hourOfDay, int minute, boolean is24HourView, int interval) {

        super(context, TimePickerDialog.THEME_HOLO_LIGHT, null, hourOfDay,
                minute / interval, is24HourView);

        TIME_PICKER_INTERVAL = interval;

        mTimeSetListener = listener;

    }

    @Override
    public void updateTime(int hourOfDay, int minuteOfHour) {
        mTimePicker.setCurrentHour(hourOfDay);
        mTimePicker.setCurrentMinute(minuteOfHour / TIME_PICKER_INTERVAL);

    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case BUTTON_POSITIVE:
                if (mTimeSetListener != null) {
                    mTimeSetListener.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
                            mTimePicker.getCurrentMinute() * TIME_PICKER_INTERVAL);
                }
                break;
            case BUTTON_NEGATIVE:
                cancel();
                break;
        }
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        try {
            Class<?> classForid = Class.forName("com.android.internal.R$id");
            Field timePickerField = classForid.getField("timePicker");
            mTimePicker = (TimePicker) findViewById(timePickerField.getInt(null));
            Field field = classForid.getField("minute");

            NumberPicker minuteSpinner = (NumberPicker) mTimePicker
                    .findViewById(field.getInt(null));
            minuteSpinner.setMinValue(0);
            minuteSpinner.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
            List<String> displayedValues = new ArrayList<String>();
            for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
                displayedValues.add(String.format("%02d", i));
            }
            minuteSpinner.setDisplayedValues(displayedValues
                    .toArray(new String[displayedValues.size()]));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I know there is at least one similar question but it doesn't have problem solving answer. Android time picker auto change hours when minutes get scrolled

EDIT: I also noticed that when I changed the minute interval to 15 it's working great. For example when I'm increasing the minutes from 45 to 00 the hours are increased by one. Same thing backwards when I'm decreasing minutes from 00 to 45 the hours are decreased by one. Same thing when the minute interval is 6. So the problem is only with the 30 minute interval and it is necessary for me to have that 30 minute interval in my software.

Nahka
  • 416
  • 1
  • 6
  • 16

1 Answers1

2

I solved it like below. Just call minute changed listener after minuteSpinner.setDisplayedValues()

minuteSpinner.setOnValueChangedListener { picker, oldVal, newVal ->
    // Declare this listener to prevent hour change 
}

I've been looking for the same problem as you and couldn't find a way. So I tried this and that by myself and found the simplest solution!

nurisis
  • 21
  • 2