37

I'd like to set max and minimum limits of SeekBar to 50 and 20 respectively.

SeekBar has a direct option top provide max value, but how to set its minimum value to 20 rather than 0?

Alexander Farber
  • 18,345
  • 68
  • 208
  • 375
Shishir.bobby
  • 10,696
  • 20
  • 67
  • 99

6 Answers6

81

In SeekBar you can set only max value.

<SeekBar android:id="@+id/SeekBar01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="50"/>

And, You cannot directly set the minimum value to the seekbar.

 SeekBar mSeekbar = (SeekBar) findViewById(R.id.SeekBar01);

    mSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    {
       public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
       {
            length_edit.setText(Integer.toString(progress + 20));
       }

      public void onStartTrackingTouch(SeekBar seekBar) {}

      public void onStopTrackingTouch(SeekBar seekBar) {}
    });

As you see min progress you can sum with min which I would like - in your case 20.

Willi Mentzel
  • 21,499
  • 16
  • 88
  • 101
Paresh Mayani
  • 122,920
  • 69
  • 234
  • 290
25

Set the max value to 30 and add 20 to the values you read from the SeekBar.

XurajB
  • 710
  • 1
  • 11
  • 28
Romain Guy
  • 95,351
  • 17
  • 214
  • 199
  • 1
    This is the only answer that does not have an effect on the desired maximum – Flynny75 May 30 '13 at 21:01
  • Since API Level 26, you can use `setMin()`: https://developer.android.com/reference/android/widget/AbsSeekBar.html#setMin(int) – Jim Apr 03 '18 at 10:16
6

The simplest way to do this is to just limit it in the SeekBar.OnSeekBarChangeListener() for your SeekBar.

First, you need to create field with min or default value. Then set this value when creating SeekBar:

private int fillDefault;

In onCreateView() of a Fragment or onCreate of an Activity:

fillDefault = 20;
fillSeekBar.setProgress(fillDefault);

Second, implement the listener for it:

fillSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            // Notification that the progress level has changed.
            if (progress < fillDefault){
                seekBar.setProgress(fillDefault); // magic solution, ha
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // Notification that the user has started a touch gesture.
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // Notification that the user has finished a touch gesture.
        }
    });

It works. Simple and awesome:D

Oleksandr Nos
  • 244
  • 3
  • 14
  • 1
    This solution is ideal when you set the padding left to 0dp and want to avoid that the thumb overlap the end of the seekbar i.e.: if(progress == 0){ seekBar.setProgress(1); } – Pedro Hidalgo Jan 28 '17 at 22:34
0

For example;

 public static int LENGTH_MIN_VALUE = 20;
 public static int LENGTH_MAX_VALUE = 50;
 SeekBar seekBar = (SeekBar) findViewById(R.id.passwordSeekBar);
    seekBar.setMax(LENGTH_MAX_VALUE);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(progress <= Constract.PASSWORD_LENGTH_MIN_VALUE){
                progress = Constract.PASSWORD_LENGTH_MIN_VALUE + progress;
            }

            String numberAsString = String.valueOf(progress);
            seekNumberView.setText(numberAsString);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {}
    });
Günay Gültekin
  • 3,469
  • 6
  • 30
  • 35
0

I have answered this question here : How to set seekbar min and max value.

With simple math you can set the min value, the max value and even the step of your seekbar.

Hope this helps !

WannaGetHigh
  • 3,519
  • 4
  • 18
  • 28
-3

You can try this:

seekBar.setMax(max_value-min_value);
MoLow
  • 2,990
  • 2
  • 17
  • 39