Search code examples
androidandroid-seekbar

Android SeekBar - how to set increment step


I want to use SeekBar to pick time. I have startDate and endDate. Difference in time is set as seekbar.max (could be for example 850 minutes(int 850)). And I want to increment this progress by 15 minutes.

How can I setup that? I tried to setup seekbar.incrementProgressBy(15) but its not working, its still incrementing by 1 minute.

What I want to achieve is that if I have 30 minutes as max and I want to move thumb button, it will automatically move to the middle so first increment is basically 15 minutes (progress 50%).

Code:

    maxDurationInMins = (((maxDate.time/1000)-(minDate.time/1000)).toInt())/60
    
    seekBar.progress = 0
    seekBar.max = maxDurationInMins
    seekBar.incrementProgressBy(15)
    seekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener{
        override fun onProgressChanged(p0: SeekBar?, progress: Int, p2: Boolean) {
            updateProgressData(progress)
        }
        override fun onStartTrackingTouch(p0: SeekBar?) {}
        override fun onStopTrackingTouch(p0: SeekBar?) {}
    })

Solution

  • As long as your max time is divisable by your increment without remainder, you could just set seekBar.max to the maximum number of increments.

    interval = 15
    seekBar.max = maxDurationInMins/interval
    

    Whenever you now react on a progress change, multiply it by the interval.