82

How to define a SeekBar's minimum value? Is this done in the XML layout or do I need to define it programatically?

Basically I need to change my minimum value from 0 to 0.2

Dharman
  • 21,838
  • 18
  • 57
  • 107
Sachin
  • 2,537
  • 8
  • 31
  • 38

13 Answers13

123

How to define a SeekBar's minimum value?

You can't define the minimum value. It is 0.

Basically I need to change my minimum value from 0 to 0.2

When you get the value, add 0.2 to it.

Dharman
  • 21,838
  • 18
  • 57
  • 107
CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • You should call `setProgress` too so the change is reflected on the UI - as Szabolcs Becze demonstrates in his answer. – ban-geoengineering Aug 09 '14 at 18:14
  • 4
    @user1617737: No, you should not. His answer has nothing to do with the minimum value. The `SeekBar` represents the range from 0 to (max-min). To get the adjusted value, you add your desired minimum to the actual progress. Since the actual progress is already "reflected on the UI", there is nothing to change there. – CommonsWare Aug 09 '14 at 18:19
  • OK, well that depends on how your labels are set up. I've just provided a complete answer which I think is the only correct way to do it. http://stackoverflow.com/questions/3033135/android-seekbar-minimum-value/25239248#25239248 – ban-geoengineering Aug 11 '14 at 08:52
  • Set the MIN label with min value and set the MAX label with max value. In onProgressChanged(), Set your current value Label as progress+minValue. – Rahul Rastogi Apr 27 '15 at 11:37
22

Here is what I use to get android:max for a max/min range for a SeekBar.

mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    int progressChanged = minimumValue;

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        progressChanged = minimumValue+ progress;
    }
});

So you will get the range minimum to minimum+max;

To set the max value in xml, use max=(desiredMax-min);

Jonno_FTW
  • 8,070
  • 7
  • 51
  • 83
Asthme
  • 4,663
  • 5
  • 44
  • 64
  • 2
    Yes I think this is the best approach. A SeekBar's units are just abstract values. You can simply adjust them in code to get the actual units you want, so I've upvoted your answer. – David George Oct 08 '15 at 12:43
9

The min value must be 0 for the SeekBar object in Java (i.e., it cannot be changed), but this is how to get the required look and performance.

Suppose you want your min to be 5 and your max to be 100...

Therefore, you have a range of 95, so you would need to set up the SeekBar with a maximum value of 95.

If your seekbar has UI labels, you would make them 5 (min) and 100 (max).

So, the layout XML would be something like...

<LinearLayout
    android:id="@+id/sampleSizeLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/batchDurationSecsLayout"
    android:orientation="vertical"
    android:visibility="visible" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/sample_size" />

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:visibility="visible" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:gravity="left"
            android:text="5" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:gravity="right"
            android:text="100" />

    </RelativeLayout>

</LinearLayout>

So now you can introduce the 5 correction, like this...

final SeekBar sampleSizeSeekBar = (SeekBar)findViewById(R.id.sampleSizeSeekBar);
final int sampleSizeSeekBarCorrection = 5; //e.g., 95 <--> 100
int realValueFromPersistentStorage = appObj.getSampleSize(); //Get initial value from persistent storage, e.g., 100
sampleSizeSeekBar.setProgress(realValueFromPersistentStorage - sampleSizeSeekBarCorrection); //E.g., to convert real value of 100 to SeekBar value of 95.
sampleSizeSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

    int val = sampleSizeSeekBar.getProgress();

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        val = progress + sampleSizeSeekBarCorrection; //e.g., to convert SeekBar value of 95 to real value of 100
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        try {
            appObj.saveSampleSize(val); //Save real value to persistent storage, e.g., 100
            appObj.makeToast("Sample size set to " + val);
        }
        catch(Exception e) {
            Log.e(LOG_TAG, "Error saving sample size", e);
            appObj.makeToast("Error saving sample size: " + e);
        }
    }
});

This solution will give your seekbar the correct min, max and scale on the UI, and the position of the control won't 'jump' to the right if the user slides it all the way to the left.

ban-geoengineering
  • 15,533
  • 18
  • 140
  • 225
9

Basically I have added the size+10 which will automatically set the min to 10 you can change yours to set it which min. value.

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    int siz = size + 10;
    txtViewFontSize.setTextSize(siz);

    Toast.makeText(this, String.valueOf(siz), Toast.LENGTH_SHORT).show();

}
Thomas Vos
  • 11,085
  • 4
  • 24
  • 63
SALMAN
  • 3,909
  • 1
  • 24
  • 21
  • Basically I have added the size+10 which will automatically set the min to 10 you can change yours to set it which min. value . – SALMAN Nov 15 '11 at 11:25
  • 10
    Please don't use comments to explain the code in your answer, please include it as part of the answer itself. – Flexo Nov 22 '11 at 17:53
6

In api versions >=26 you can now add min xml attribute to SeekBar:

android:min="0.2"

João Magalhães
  • 2,275
  • 2
  • 11
  • 16
5

My solution which works fine for me is:

    @Override 
 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if (progress <= SOME_LIMIT) {
        seekBar.setProgress(SOME_LIMIT);
    } else {
       // DO SOMETHING
    }

} 
Szabolcs Becze
  • 396
  • 4
  • 9
4

You can not set progress in float because seekbar.getProgress() always returns integer value. Following code will set textview value to 0.2 if seekbar value is 0. you can change minimumVal according to your requirement.

int minimumVal = 0;

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {


                if (progress >= minimumVal) {
                    seekBar.setProgress(progress);
                    textView.setText(progress);
                } else {
                    seekBar.setProgress(minimumVal);
                    textView.setText("0.2");
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
Dharmesh
  • 211
  • 2
  • 3
3

I created this simple library, it's not much, but it can save you some time.

https://github.com/joaocsousa/DoubleSeekBar

Joao Sousa
  • 3,731
  • 1
  • 22
  • 25
1

You can try doing this way:

int yourMinProgress = 10;

private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener =
    new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {

            if (progress < yourMinProgress) {
                seekBar.setProgress(10);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };
Tunaki
  • 116,530
  • 39
  • 281
  • 370
Sasha
  • 33
  • 6
0

this code works for me fine!!!

SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setMax(100);
seekBar.setProgress(40);
seekBar.setOnSeekBarChangeListener(seekBarChangeListener);

SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
        if(progress <= 40){
            seekBar.setProgress(40);
        }
        if(progress >= 40) {
            pgr_status.setText(String.valueOf(progress));
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
};
Karthick
  • 251
  • 2
  • 7
0

I finally found a way to set seekBar minimum value, just set set your setOnSeekBarChangeListener like this:

int minimumValue = 10; // your minimum value
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // you can set progress value on TextView as usual
        txtProgress.setText(String.valueOf(progress));

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if(seekBar.getProgress() < minimumValue)
            seekBar.setProgress(minimumValue);

    }
});

Hope this helps :)

Behzad Bahmanyar
  • 5,696
  • 4
  • 29
  • 38
0

Here is how i did this with Kotlin:

view.controlTextSize.setOnClickListener {

        textSizeControl.textSizeControlSlider.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{

            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                var progress = progress
                if (progress < 10){
                    progress = 10
                    seekBar?.progress = progress
                }

                view.content.textSize = progress.toFloat()
                prefs.edit().putFloat("ContentTextSize", progress.toFloat()).apply()
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {}

            override fun onStopTrackingTouch(seekBar: SeekBar?) {}
        })
        textSizeControl.show()
    }
Riajul
  • 732
  • 11
  • 18
0

If you are using com.android.support:preference-v7, SeekBarPreference already has a method setMin(int).

Just call that in onCreatePreferences() of your PreferenceFragmentCompat.

XML values will still be ignored on API < 26 but setting it programatically, works.

hoshiKuzu
  • 715
  • 1
  • 9
  • 25