6

I've followed the tutorials for incrementing the Android TimePicker to intervals other than 15.

Android - TimePicker minutes to 15

I have in fact properly set up my timePicker to increment in intervals of 5. However, when I'm not scrolling through the TimePicker the previous and subsequent minutes displayed are not in increments of 5. They are single digit increments. Is there a way to edit previous and subsequent minutes in Android TimePicker?

Here is an image of the TimePicker. When you scroll through the minutes the middle number jumps in increments of 5, as I would like, but the minute above and below that middle number (the currentMinute) are not in increments of 5 and I would like them to be also.

enter image description here

Community
  • 1
  • 1
Hilary
  • 61
  • 5
  • I also need to know about this. If anyone can help I'd really appreciate it. – Marky Jun 16 '13 at 12:31
  • @Marky it can be done but it's very hacky and I would say unreliable. Use one of the implementation ports and use that to make the changes. – user Jun 21 '13 at 07:18
  • @Hilary did you find a solution ? I have the same problem. I didn't test the 01.sunlit's solution because NumberPicker class is added in api level 11 and it is not in android-support-v4 – mrroboaat Jul 29 '13 at 15:31

2 Answers2

0

This solutions also works with listeners and when setting time programmatically.

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
    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 = 0
            mMinuteSpinner.maxValue = 60 / 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)
    }

    companion object {
        @JvmStatic
        @BindingAdapter("time_picker_set_minute_interval")
        fun setInterval(view: TimePickerInterval, interval: Int?) {
            interval?.let {
                view.MINUTE_INTERVAL = interval
                view.setUp()
            }
        }
    }
}

In your XML:

    <com.gixon.shared.widgets.TimePickerInterval
        android:id="@+id/tp_to"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:timePickerMode="spinner"
        time_picker_set_minute_interval="@{5}"
        />
-1

all relative answer need you to set an OnTimeChangedListener. My resolution is that you extends android TimePicker,and modify the constructor of it:

// minute
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
mMinuteSpinner.setMinValue(0);
mMinuteSpinner.setMaxValue(3);
mMinuteSpinner.setDisplayedValues(new String[]{"0", "15", "30", "45"});
mMinuteSpinner.setOnLongPressUpdateInterval(100);
mMinuteSpinner.setFormatter(NumberPicker.getTwoDigitFormatter());

so you can have the interval you want.

01.sunlit
  • 305
  • 1
  • 2
  • 8