6

I'm trying to set a 10 minute interval in the android DialogFragment(suggested by google), but I really can't find a way to do it.

Here is the fragment class wich shows the dialog:

public class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        String realMinute= "";

        switch (minute) {
            case 1:
                realMinute = "01";
                break;
            case 2:
                realMinute = "02";
                break;
            case 3:
                realMinute = "03";
                break;
            case 4:
                realMinute = "04";
                break;
            case 5:
                realMinute = "05";
                break;
            case 6:
                realMinute = "06";
                break;
            case 7:
                realMinute = "07";
                break;
            case 8:
                realMinute = "08";
                break;
            case 9:
                realMinute = "09";
                break;
            default:
                realMinute = String.valueOf(minute);
                break;
        }

        final String selectedDate = String.valueOf(hourOfDay) + ":" + realMinute;
        EditText selectedTimeTxt = (EditText) getActivity().findViewById(R.id.selectedTime);
        selectedTimeTxt.setText(selectedDate);
    }

Everything works as expected, but what I need is to allow the user to pick only intervals of 10 minutes - for example 1:10, 1:20, 1:30, 1:40 etc...

Is it possible to modify it, or extend it somehow in order to fit my needs? I know that I'm missing a basic point here, but as a newbie I'm not able to spot it.

EDIT

Here my views are:

enter image description here

Slim
  • 1,708
  • 5
  • 37
  • 60
  • 1
    A quick Google search let me to this answer: http://stackoverflow.com/questions/7713538/how-to-set-a-custom-minutes-interval-in-timepickerdialog-in-android/19970113#19970113 – cYrixmorten Feb 07 '15 at 13:48
  • @cYrixmorten I have tried it and it is not working. it just multiply the minutes by the interval value. Also there is no visual change on teh picker. :( – Slim Feb 07 '15 at 14:07
  • How about creating your own custom timepickerdialog using TimePicker views such as http://stackoverflow.com/questions/2580216/android-timepicker-minutes-to-15? TimePicker allows you to set which values it has, so you could have ["10", "20", "30",..] as options for minutes – cYrixmorten Feb 07 '15 at 14:15

1 Answers1

4

You can set your own values to the 2nd number picker (the minute number picker), with a call of setDisplayedValues. However you first need to find the NumberPicker view.

Android Device Monitor, has a nice feature of capturing the device's screen and the separate views, so you can find the id (or a path) to your wanted view:

enter image description here

Since your new value array holds 6 values (0...5), you will need to convert the current minutes to tens, so you don't go out of range.

Here's a complete example:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    // Convert current minutes to tens
    // 55 = 50, 56 = 00
    int minute = c.get(Calendar.MINUTE) / 10;
    minute = (minute > 5) ? 0 : minute;

    // Create a new instance of TimePickerDialog and return it
    final TimePickerDialog tpd = new TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity()));

    tpd.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            int tpLayoutId = getResources().getIdentifier("timePickerLayout", "id", "android");

            ViewGroup tpLayout = (ViewGroup) tpd.findViewById(tpLayoutId);
            ViewGroup layout = (ViewGroup) tpLayout.getChildAt(0);

            // Customize minute NumberPicker
            NumberPicker minutePicker = (NumberPicker) layout.getChildAt(2);
            minutePicker.setDisplayedValues(new String[]{"00", "10", "20", "30", "40", "50"});
            minutePicker.setMinValue(0);
            minutePicker.setMaxValue(5);
        }
    });

    return tpd;
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    minute = minute * 10;
    Toast.makeText(getActivity(), "Selected minute: " + minute, Toast.LENGTH_LONG).show();
}

Edit:

When using AppCompat theme, the dialog will show a circular slider, which is uncustomizable. However you can tell the dialog to use a newer theme (which won't be a problem because your minSdk is 15):

final TimePickerDialog tpd = new TimePickerDialog(getActivity(),
        android.R.style.Theme_Holo_Light_Dialog, this, hour, minute,
        DateFormat.is24HourFormat(getActivity()));
Simas
  • 43,548
  • 10
  • 88
  • 116
  • There is an error `Attempt to invoke virtual method 'android.view.View android.view.ViewGroup.getChildAt(int)' on a null object reference` at `ViewGroup layout = (ViewGroup) tpLayout.getChildAt(0);` :( – Slim Feb 09 '15 at 07:13
  • @Slim are you using the exact same code as in my answer? Your error could mean the layout is not yet inflated or it's different from the one my `TimePickerDialog` has. Confirm which one it is, by using the method I posted (with Android Device Monitor). – Simas Feb 09 '15 at 07:35
  • @Slim looks like you're using an AppCompat theme which provides a different TimePickerDialog. What API are you using? – Simas Feb 09 '15 at 07:48
  • `minSdkVersion: 15`, `targetSdkVersion: 21`, `compileSdkVersion: 21` on a Lolipop emulator – Slim Feb 09 '15 at 07:51
  • @Slim then there's no need for you to use the AppCompat theme. In your styles.xml change to ` – Simas Feb 09 '15 at 07:54
  • @Slim what you're seeing is the old design of a `TimePicker`. It contains a circular slider which is a single view and is not customizable. You're better off using the new TimePicker or creating your own from scratch. – Simas Feb 09 '15 at 08:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70547/discussion-between-slim-and-user3249477). – Slim Feb 09 '15 at 08:07