9

how can I get the values ​​from 0.0 to 2.0 with a seekbar? values ​​should go in steps of 0.5 ( 0.0 , 0.5 , 1.0, 1.5, 2.0) can someone help me?

kosma822
  • 183
  • 1
  • 2
  • 7

3 Answers3

20

SeekBar extends ProgressBar.

ProgressBar has a method

public void setMax(int max)

Since it only accepts int you'll have to do some conversion from an int value to get the float that you are after.

Something like this should do the trick:

mSeekBar.setMax(4);
//max = 4 so the possible values are 0,1,2,3,4
seekbar.setOnSeekBarChangeListener( new OnSeekBarChangeListener(){
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
    {
        // TODO Auto-generated method stub
        Toast.makeText(seekBar.getContext(), "Value: "+getConvertedValue(progress), Toast.LENGTH_SHORT).show();
    }
    public void onStartTrackingTouch(SeekBar seekBar)
    {
        // TODO Auto-generated method stub
    }
    public void onStopTrackingTouch(SeekBar seekBar)
    {
        // TODO Auto-generated method stub
    }
});

This part will be in your onCreate() and you'll have to use findViewById() or something to get a reference to mSeekBar.

public float getConvertedValue(int intVal){
    float floatVal = 0.0;
    floatVal = .5f * intVal;
    return floatVal;
}

This part is another method that you can add to your Activity that will convert from int to float values within the range you need.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • good, thanks!! now i need to set seekbar progress, but .progress function accept only int values, how can I convert the float to int in this project? – kosma822 Mar 28 '13 at 03:24
  • @kosma822 you use the setProgress(int) method. When you call it, it should in turn call the onProgressChange() method in your listener which will do the conversion to float for you with that method that is the second chunk of code in my answer. Just change the Toast in my example to whatever it is you want to do with the float value. – FoamyGuy Mar 28 '13 at 13:16
3
     textView.setProgress(0);
     textView.incrementProgressBy(1);
     textView.setMax(4);

       public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) { 

                       float progressD=(float) progress/2;
                       textView.setText(String.valueOf(progressD));

// from 0 to 2 step 0.5

Manuelvalles
  • 380
  • 2
  • 6
0

You should implement your own custom seek bar, make it jump between the desired values and stick to them, you could find useful info here:

Making A Custom Skinny ProgressBar / Seekbar

Community
  • 1
  • 1
Kirill Kulakov
  • 10,035
  • 9
  • 50
  • 67
  • That question deals more with customizing the look of the SeekBar rather than the functionality of the values that are possible. To get the effect that OP is after there is no need to make a custom seek bar. There just needs to be a conversion from int to float for the range of values that they are needing. – FoamyGuy Mar 26 '13 at 21:37
  • I dont need a custom layout, i neet to do it programmatically, and i need a example :/ – kosma822 Mar 26 '13 at 21:51