Fearghal
Fearghal

Reputation: 11407

Timepicker with 15 minute intervals

I have a custom dialog with a timepicker and a datepicker. I want to set the timepicker's minute spinner collection to [0, 15, 30, 45] ie 15 min intervals.

Now i see several solutions but none seem to cover the fact that the UI will show the next and previous minutes as -1 and +1 minutes of selected minute eg 29, 30, 31 are shown to user.

Also, im not clear on how i attach my custom timepicker to an activity - do i not need a customerTimePicker xml component to do this?

So in short - do i adjust the native timepicker or need to build a custom xml and class?

What i have at present Layout:

<TimePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/timePicker_fixture"
    android:layout_weight="1" />

Activity

TimePicker timePicker_fixture = (TimePicker) dialog.findViewById(R.id.timePicker_fixture);
timePicker_fixture.setIs24HourView(true);

A suggested custom class (how do i use this - do i need a custom xml component in my layout?)

public class CustomTimePickerDialog extends TimePickerDialog {

    private final static int TIME_PICKER_INTERVAL = 15;
    private TimePicker timePicker;
    private final OnTimeSetListener callback;

    public CustomTimePickerDialog(Context context, OnTimeSetListener callBack,
                                  int hourOfDay, int minute, boolean is24HourView) {
        super(context, TimePickerDialog.THEME_HOLO_LIGHT, callBack, hourOfDay, minute / TIME_PICKER_INTERVAL,
                is24HourView);
        this.callback = callBack;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (callback != null && timePicker != null) {
            timePicker.clearFocus();
            callback.onTimeSet(timePicker, timePicker.getCurrentHour(),
                    timePicker.getCurrentMinute() * TIME_PICKER_INTERVAL);
        }
    }

    @Override
    protected void onStop() {
    }

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

            NumberPicker mMinuteSpinner = (NumberPicker) timePicker
                    .findViewById(field.getInt(null));
            mMinuteSpinner.setMinValue(0);
            mMinuteSpinner.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));
            }
            mMinuteSpinner.setDisplayedValues(displayedValues
                    .toArray(new String[0]));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Upvotes: 6

Views: 12446

Answers (4)

Tharik ahamed
Tharik ahamed

Reputation: 159

/**
 * Set TimePicker interval by adding a custom minutes list
 *
 * @param timePicker
 */
private void setTimePickerInterval(TimePicker timePicker) {
    try {
        int TIME_PICKER_INTERVAL = 15;
        NumberPicker minutePicker = (NumberPicker) timePicker.findViewById(Resources.getSystem().getIdentifier(
                "minute", "id", "android"));
        minutePicker.setMinValue(0);
        minutePicker.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));
        }
        minutePicker.setDisplayedValues(displayedValues.toArray(new String[0]));
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e);
    }
}

Upvotes: 1

Adri&#225;n Rivero
Adri&#225;n Rivero

Reputation: 161

The answer above didn't work for me in Android 7 Nougat, basically the date picker is changed completely by the Clock mode... I kind of merge this code with https://gist.github.com/jeffdgr8/6bc5f990bf0c13a7334ce385d482af9f, then it set by default the mode of the TimePickerDialog to show the Spinner controls, and then it runs the code to show only 15minuts intervals.

The result is here: https://gist.github.com/smaugho/14dae83f3284fa05455ee0a9e4f13099

Upvotes: 0

maros136
maros136

Reputation: 346

timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener()
{
    @Override
    public void onTimeChanged (TimePicker view, int hourOfDay, int minute)
    {
        view.setOnTimeChangedListener(null);
        int currentMinute = roundMinute(minute);
        view.setCurrentMinute(59);
        view.setCurrentMinute(currentMinute);
        view.setOnTimeChangedListener(this);
    }
});

private static int roundMinute (int minute)
{
    return minute == 0 ? 0 : minute/5*5;
}

Upvotes: -1

Fearghal
Fearghal

Reputation: 11407

Added the following

   @SuppressLint("NewApi")
    private void setTimePickerInterval(TimePicker timePicker) {
        try {
            Class<?> classForid = Class.forName("com.android.internal.R$id");
            // Field timePickerField = classForid.getField("timePicker");

            Field field = classForid.getField("minute");
            minutePicker = (NumberPicker) timePicker
                    .findViewById(field.getInt(null));

            minutePicker.setMinValue(0);
            minutePicker.setMaxValue(7);
            displayedValues = new ArrayList<String>();
            for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
                displayedValues.add(String.format("%02d", i));
            }
            for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
                displayedValues.add(String.format("%02d", i));
            }
            minutePicker.setDisplayedValues(displayedValues
                    .toArray(new String[0]));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

in my activity and then called this method from oncreate(). Of course you can call it from wherever necessary.

Upvotes: 4

Related Questions