How to set custom minute interval in TimePickerDialog on Android

I have a TimePickerDialog working on setting the time that is set in the TextView to display it. Now I need help to set the TimePicker timeout (inside TimePickerDialog) for 15 minutes. I saw that there is a message with an interval of 15 minutes associated with TimePicker, but I do not know how to apply it to TimePickerDialog, because I do not know how to use the TimePicker, which it creates inside TimePickerDialog. I am new to Android and completely lost in this matter. Thanks in advance.

+4
source share
4 answers

Using a combination of this from @Rizwan and this in another topic, I came up with a combined solution that allows arbitrary minute increments in TimePickerDialog . The main problem is that most of the functions are hidden by the android classes TimePickerDialog and TimePicker , and this is not like

  • Extend TimePickerDialog to facilitate access.
  • Use reflection to reach inside the display and access the required bits (see below).
  • rename the minute "NumberPicker" to display our values
  • reinstall TimePicker to get and return values โ€‹โ€‹from NumberPicker in compliance with our custom increment.
  • lock onStop () so that it does not reset the value when closed.

A warning

The main problem with reaching inside the user interface is that elements are referenced by identifiers, which may change, and even the identifier name is not guaranteed forever. Having said that, it works, a stable solution, and is likely to work in the foreseeable future. In my opinion, an empty blocking block should warn that the user interface has changed and should return to default (in 1 minute increments).

Decision

  private class DurationTimePickDialog extends TimePickerDialog { final OnTimeSetListener mCallback; TimePicker mTimePicker; final int increment; public DurationTimePickDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, int increment) { super(context, callBack, hourOfDay, minute/increment, is24HourView); this.mCallback = callBack; this.increment = increment; } @Override public void onClick(DialogInterface dialog, int which) { if (mCallback != null && mTimePicker!=null) { mTimePicker.clearFocus(); mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute()*increment); } } @Override protected void onStop() { // override and do nothing } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { Class<?> rClass = Class.forName("com.android.internal.R$id"); Field timePicker = rClass.getField("timePicker"); this.mTimePicker = (TimePicker)findViewById(timePicker.getInt(null)); Field m = rClass.getField("minute"); NumberPicker mMinuteSpinner = (NumberPicker)mTimePicker.findViewById(m.getInt(null)); mMinuteSpinner.setMinValue(0); mMinuteSpinner.setMaxValue((60/increment)-1); List<String> displayedValues = new ArrayList<String>(); for(int i=0;i<60;i+=increment) { displayedValues.add(String.format("%02d", i)); } mMinuteSpinner.setDisplayedValues(displayedValues.toArray(new String[0])); } catch (Exception e) { e.printStackTrace(); } } } } 

the constructor takes an increment value and saves some other references. Note that this excludes error checking and we would prefer 60%increment==0

onCreate uses the interface field name and reflection to locate the current location. Again, this eliminates error checking and should be "fault tolerant", i.e. revert to default behavior if something goes wrong.

onClick is overridden to return the correct minute value to the callback listener

onStop is redefined to prevent the (incorrect) index value from returning a second time the dialog closes. Go ahead, try it yourself.

Most of this comes from what you dig into the source TimePickerDialog.

+13
source

Well, thatโ€™s fine if you used a timer and not a timing dialog. But there is actually a solution for this .. here is what I used to satisfy the same requirement .. I used CustomTimePickerDialog. Each thing will be the same, only the TimePickerDialog will change to CustomTimePickerDialog in the code.

 CustomTimePickerDialog timePickerDialog = new CustomTimePickerDialog(myActivity.this, timeSetListener, Calendar.getInstance().get(Calendar.HOUR), CustomTimePickerDialog.getRoundedMinute(Calendar.getInstance().get(Calendar.MINUTE) + CustomTimePickerDialog.TIME_PICKER_INTERVAL), false ); timePickerDialog.setTitle("2. Select Time"); timePickerDialog.show(); 

Here is my CustomTimePickerDialog class ... Just use this class in your project and change TimePickerDialog to CustomTimePickerDialog ..

 public class CustomTimePickerDialog extends TimePickerDialog{ public static final int TIME_PICKER_INTERVAL=10; private boolean mIgnoreEvent=false; public CustomTimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) { super(context, callBack, hourOfDay, minute, is24HourView); } /* * (non-Javadoc) * @see android.app.TimePickerDialog#onTimeChanged(android.widget.TimePicker, int, int) * Implements Time Change Interval */ @Override public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute) { super.onTimeChanged(timePicker, hourOfDay, minute); this.setTitle("2. Select Time"); if (!mIgnoreEvent){ minute = getRoundedMinute(minute); mIgnoreEvent=true; timePicker.setCurrentMinute(minute); mIgnoreEvent=false; } } public static int getRoundedMinute(int minute){ if(minute % TIME_PICKER_INTERVAL != 0){ int minuteFloor = minute - (minute % TIME_PICKER_INTERVAL); minute = minuteFloor + (minute == minuteFloor + 1 ? TIME_PICKER_INTERVAL : 0); if (minute == 60) minute=0; } return minute; } } 

After using this CustomTimePickerDialog class, all you have to do is use CustomTimePickerDialog instead of TimePickerDialog in your code to access / override the standard functions of the TimePickerDialog class. In a simple way, I mean that your timeSetListener will be next after this ...

 private CustomTimePickerDialog.OnTimeSetListener timeSetListener = new CustomTimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { } }// using CustomTimePickerDialog 
+8
source

You can use the usual AlertDialog and use setView to include a custom TimePicker view.

+1
source
 /** * Set TimePicker interval by adding a custom minutes list * TIME_PICKER_INTERVAL = Enter your Minutes; * @param timePicker */ private void setTimePickerInterval(TimePicker timePicker) { try { int TIME_PICKER_INTERVAL = 10; 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); } } 
0
source

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


All Articles