how to limit seekbar how to limit seekbar android android

how to limit seekbar


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.


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


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