Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set seekbar min and max value

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.progresss);         sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {                     @Override                    public void onStopTrackingTouch(SeekBar seekBar) {                       // TODO Auto-generated method stub                 //int inVal = Integer.parseInt(String.valueOf(seekBar.getProgress()));                 //inVal =+ 70;                 //Toast.makeText(getApplicationContext(), String.valueOf(inVal),Toast.LENGTH_LONG).show();             }                     @Override                    public void onStartTrackingTouch(SeekBar seekBar) {                      // TODO Auto-generated method stub                   }                     @Override                    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {                      // TODO Auto-generated method stub                 progress =+ 70;                 Toast.makeText(getApplicationContext(), String.valueOf(progress),Toast.LENGTH_LONG).show();              }                }); 

is not working. Any idea how to fix it?

like image 323
Si8 Avatar asked Dec 24 '13 13:12

Si8


People also ask

What is SeekBar ProgressBar?

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged. Clients of the SeekBar can attach a SeekBar.

How do I set the value of SeekBar?

One possible solution would be to set seekBar. setMax(20) (or android:max="20" in XML), and whenever you use or display the value, multiply it by 10. The SeekBar would then appear to move at least 20 at a time.

How do you set the minimum and maximum value of SeekBar in android programmatically?

setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub //int inVal = Integer. parseInt(String. valueOf(seekBar. getProgress())); //inVal =+ 70; //Toast.

How do you set max and min in SeekBar?

In order to set the max value in java file you can use the function setMax(INTEGER) on seekbar object to set the max value. seekBar = (SeekBar) findViewById(R. id. seekBar1); seekBar.


2 Answers

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.

like image 99
WannaGetHigh Avatar answered Sep 19 '22 22:09

WannaGetHigh


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 subtract 50 from the raw value to get the number you should use when setting the bar position.

You can get more info via this link.

like image 32
Kailash Dabhi Avatar answered Sep 22 '22 22:09

Kailash Dabhi