Android SeekBar Minimum Value Android SeekBar Minimum Value android android

Android SeekBar Minimum Value


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.


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);


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 <--> 100int realValueFromPersistentStorage = appObj.getSampleSize(); //Get initial value from persistent storage, e.g., 100sampleSizeSeekBar.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.