Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Seekbar float values from 0.0 to 2.0

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?

like image 804
kosma822 Avatar asked Mar 26 '13 21:03

kosma822


2 Answers

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.

like image 99
FoamyGuy Avatar answered Sep 30 '22 17:09

FoamyGuy


     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

like image 42
Manuelvalles Avatar answered Sep 30 '22 16:09

Manuelvalles