How to set minute interval in DialogFragment

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

Here is a snippet that 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 I need the user to be able to select only intervals of 10 minutes - for example, 1:10, 1:20, 1:30, 1:40, etc.

Can it be modified or expanded in some way to fit my needs? I know that I do not have the main point here, but as a beginner I can not notice it.

EDIT

Here are my views:

enter image description here

+6
source share
1 answer

You can set your own values ​​for the second set of numbers (select the number of minutes) with a call to setDisplayedValues . However, first you need to find the NumberPicker .

Android Device Monitor has a nice ability to capture the device screen and individual views, so you can find the identifier (or path) to your desired view:

enter image description here

Since your new array of values ​​contains 6 values ​​(0 ... 5), you will need to convert the current minutes to tens so that you do not go out of range.

Here is 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 the AppCompat theme, a circular slider is displayed in the dialog box that cannot be described. However, you can tell the dialog to use a newer theme (which will not 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())); 
+4
source

Source: https://habr.com/ru/post/982042/


All Articles