5

I am working on DatePicker and TimePicker in android. I am getting start time and and time from server. I can set max date and min date programmatically using this link

I want to set min time (07:00 ) and max time (10:00) in timepicker and maintain an interval of 30 minutes. I tried using This Link

What i need is to set timepicket with min 07:00 and max time =10:00 and intervals 07:00,07:30, 08:00 etc.

Here is the code same `

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TimePicker pn = (TimePicker) findViewById(R.id.tp_powernap); pn.setCurrentHour(7); pn.setCurrentMinute(0); System.out.println("timepicker erstellt"); pn.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS); System.out.println("Tastatur geblockt"); pn.setOnTimeChangedListener(StartTimeChangedListener); System.out.println("starttimechangedlistener"); pn.setIs24HourView(false); } private TimePicker.OnTimeChangedListener StartTimeChangedListener = new TimePicker.OnTimeChangedListener() { public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { updateDisplay(view, hourOfDay, minute); } }; private TimePicker.OnTimeChangedListener NullTimeChangedListener = new TimePicker.OnTimeChangedListener() { public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { } }; private void updateDisplay(TimePicker timePicker, int hourOfDay, int minute) { int nextMinute = 0; timePicker.setOnTimeChangedListener(NullTimeChangedListener); if (minute >= 45 && minute <= 59) nextMinute = 45; else if (minute >= 30) nextMinute = 30; else if (minute >= 15) nextMinute = 15; else if (minute > 0) nextMinute = 0; else { nextMinute = 45; } if (minute - nextMinute == 1) { if (minute >= 45 && minute <= 59) nextMinute = 00; else if(minute >= 30) nextMinute = 45; else if(minute >= 15) nextMinute = 30; else if(minute > 0) nextMinute = 15; else { nextMinute = 15; } } timePicker.setCurrentMinute(nextMinute); timePicker.setOnTimeChangedListener(StartTimeChangedListener); } 

2 Answers 2

1

Just for reference (this is a old question). The interval can be implemented using the following hack:

Class<?> classForid = Class.forName("com.android.internal.R$id"); 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])); 

see also here

Start- and Endtime can be set in similar manner but there are problems with the AM/PM Flag. If you don't use am/pm but instead always show the time in 24-hours format your problem is solved ;-)

 if (startHour != -1 && endHour != -1) { Field field = classForid.getField("hour"); NumberPicker hourSpinner = (NumberPicker) timePicker .findViewById(field.getInt(null)); hourSpinner.setMinValue(startHour); hourSpinner.setMaxValue(endHour); List<String> displayedValues = new ArrayList<String>(); if (timePicker.is24HourView()) { for (int i = startHour; i <= endHour; ++i) { displayedValues.add(String.format("%02d", i)); } } else { for (int i = startHour; i <= 12; ++i) { displayedValues.add(String.format("%d", i)); } for (int i = 1; i <= endHour % 12; ++i) { displayedValues.add(String.format("%d", i)); } } hourSpinner.setDisplayedValues(displayedValues .toArray(new String[0])); } 
Sign up to request clarification or add additional context in comments.

Comments

0

Try this code.

import android.content.Context import android.util.AttributeSet import android.widget.NumberPicker import android.widget.TimePicker import androidx.databinding.BindingAdapter import java.lang.reflect.Field import java.util.* class TimePickerInterval @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : TimePicker(context, attrs, defStyleAttr) { private var MINUTE_INTERVAL = 1 private var MIN_MINUTE= 0 private var MAX_MINUTE = 60 override fun getCurrentMinute(): Int { return super.getCurrentMinute() * MINUTE_INTERVAL } override fun getMinute(): Int { return super.getMinute() * MINUTE_INTERVAL } override fun onAttachedToWindow() { super.onAttachedToWindow() setUp() } private fun setUp() { try { val classForid = Class.forName("com.android.internal.R\$id") val field: Field = classForid.getField("minute") val mMinuteSpinner = findViewById<NumberPicker>(field.getInt(null)) mMinuteSpinner.minValue = MIN_MINUTE mMinuteSpinner.maxValue = MAX_MINUTE / MINUTE_INTERVAL - 1 val displayedValues: MutableList<String> = ArrayList() var i = 0 while (i < 60) { displayedValues.add(String.format("%02d", i)) i += MINUTE_INTERVAL } mMinuteSpinner.displayedValues = displayedValues.toTypedArray() } catch (e: Exception) { e.printStackTrace() } } override fun setOnTimeChangedListener(onTimeChangedListener: OnTimeChangedListener) { super.setOnTimeChangedListener { tp, hour, minute -> onTimeChangedListener.onTimeChanged(tp, hour, minute * MINUTE_INTERVAL) } } override fun setMinute(minute: Int) { super.setMinute(minute / MINUTE_INTERVAL) } fun setMinMinute(min: Int) { MIN_MINUTE = min setUp() } fun setMaxMinute(max: Int) { MAX_MINUTE = max setUp() } fun setMinuteInterval(interval: Int) { MINUTE_INTERVAL = interval setUp() } companion object { @JvmStatic @BindingAdapter("time_picker_set_minute_interval") fun setMinuteInterval(view: TimePickerInterval, interval: Int?) { interval?.let { view.setMinuteInterval(it) } } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.