How to set seekbar min and max value

前端 未结 14 827
栀梦
栀梦 2020-11-28 09:17

I have a seekbar and trying to set the value from 60 to 180 for one and 40 to 190 for the second one in step of 1.

sb1 = (SeekBar) findViewById(R.id.progress         


        
相关标签:
14条回答
  • 2020-11-28 09:46

    You can set max value for your seekbar by using this code:

        sb1.setMax(100);
    

    This will set the max value for your seekbar.

    But you cannot set the minimum value but yes you can do some arithmetic to adjust value. Use arithmetic to adjust your application-required value.

    For example, suppose you have data values from -50 to 100 you want to display on the SeekBar. Set the SeekBar's maximum to be 150 (100-(-50)), then add 50 to the raw value to get the number you should use when setting the bar position.

    You can get more info via this link.

    0 讨论(0)
  • 2020-11-28 09:53

    You cannot set the min value of a SeekBar (always 0) and you cannot set the step value of a SeekBar (always 1).

    To set the value from 60 to 180 with a step of 1:

    int step = 1;
    int max = 180;
    int min = 60;
    
    // Ex : 
    // If you want values from 3 to 5 with a step of 0.1 (3, 3.1, 3.2, ..., 5)
    // this means that you have 21 possible values in the seekbar.
    // So the range of the seek bar will be [0 ; (5-3)/0.1 = 20].
    seekbar.setMax( (max - min) / step );
    
    
    seekbar.setOnSeekBarChangeListener(
        new OnSeekBarChangeListener()
        {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
    
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, 
                boolean fromUser) 
            {
                // Ex :
                // And finally when you want to retrieve the value in the range you
                // wanted in the first place -> [3-5]
                //
                // if progress = 13 -> value = 3 + (13 * 0.1) = 4.3
                double value = min + (progress * step);
    
            }
        }
    );
    

    I put another example within the code so that you understand the math.

    0 讨论(0)
  • 2020-11-28 09:54

    The easiest way to set a min and max value to a seekbar for me: if you want values min=60 to max=180, this is equal to min=0 max=120. So in your seekbar xml set property:

    android:max="120"
    

    min will be always 0.

    Now you only need to do what your are doing, add the amount to get your translated value in any change, in this case +60.

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int translatedProgress = progress + 60;
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    

    Be careful with the seekbar property android:progress, if you change the range you must recalculate your initial progress. If you want 50%, max/2, in my example 120/2 = 60;

    0 讨论(0)
  • 2020-11-28 09:57

    Min-value will always start at zero and its nothing you can do about it. But you can change its value when user start scrolling it around.

    Here I set the max-value as 64. This calculations are simple: I want the user to pick a time from 15min to 16 hours, and he picks one of every 15min to 16 hours, clear? I know, very simple :)

        SeekBar seekBar = (SeekBar) dialog.findViewById(R.id.seekBar);
        seekBar.setMax(64);
    
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            float b;
    
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float des = (float) progress / 4;
                b = des;
                hours.setText(des + " hours");
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
    
    
            }
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                hoursSelected = b;
            }
        });
    
    0 讨论(0)
  • 2020-11-28 09:58

    Seek Bar has methods for setting max values but not for setting min value here i write a code for setting minimum seek bar value when we add this code then your seek bar values not less then mim value try this its work fine for me

    /* This methods call after seek bar value change */

                public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                /* Check the current seekbar value is greather than min value*/
                if (progress < MIN_VALUE) {
                    /* if seek bar value is lesser than min value then set min value to seek bar */
                    seekBar.setProgress(MIN_VALUE);
                }
    
            }
    
    0 讨论(0)
  • 2020-11-28 09:59

    Another solution to handle this case is creating a customized Seekbar, to get ride of converting the real value and SeekBar progress every time:

    import android.content.Context
    import android.util.AttributeSet
    import android.widget.SeekBar
    
    //
    // Require SeekBar with range [Min, Max] and INCREMENT value,
    // However, Android Seekbar starts from 0 and increment is 1 by default, Android supports min attr on API 26,
    // To make a increment & range Seekbar, we can do the following conversion:
    //
    //     seekbar.setMax((Max - Min) / Increment)
    //     seekbar.setProgress((actualValue - Min) / Increment)
    //     seekbar.getProgress = Min + (progress * Increment)
    //
    // The RangeSeekBar is responsible for handling all these logic inside the class.
    
    data class Range(val min: Int, val max: Int, private val defaultIncrement: Int) {
        val increment = if ((max - min) < defaultIncrement) 1 else defaultIncrement
    }
    
    
    internal fun Range.toSeekbarMaximum(): Int = (max - min) / increment
    
    
    class RangeSeekBar: SeekBar, SeekBar.OnSeekBarChangeListener {
    
        constructor(context: Context) : super(context)
        constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    
        var range: Range = Range(0, 100, 1)
            set(value) {
                field = value
                max = value.toSeekbarMaximum()
            }
    
        var value: Int = 0
            get() = range.min + progress * range.increment
            set(value) {
                progress = (value - range.min) / range.increment
                field = value
            }
    
        var onSeekBarChangeListenerDelegate: OnSeekBarChangeListener? = this
    
        override fun setOnSeekBarChangeListener(l: OnSeekBarChangeListener?) {
            onSeekBarChangeListenerDelegate = l
            super.setOnSeekBarChangeListener(this)
        }
    
        override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
            onSeekBarChangeListenerDelegate?.onProgressChanged(seekBar, value, fromUser)
        }
    
        override fun onStartTrackingTouch(seekBar: SeekBar?) {
            onSeekBarChangeListenerDelegate?.onStartTrackingTouch(seekBar)
        }
    
        override fun onStopTrackingTouch(seekBar: SeekBar?) {
            onSeekBarChangeListenerDelegate?.onStopTrackingTouch(seekBar)
        }
    }
    

    Then in your fragment,

        // init
        range_seekbar.range = Range(10, 110, 10)
        range_seekbar.value = 20
    
        // observe value changes
        range_seekbar.userChanges().skipInitialValue().subscribe {
            println("current value=$it")
        }
    

    Keywords: Kotlin, range SeekBar, Rx

    0 讨论(0)
提交回复
热议问题