33

I have a seekbar for example: from 0 to 10. How can I make if I want to set seekbar to 5 as default position. So in this example seekbar should start from middle.

Adam
  • 2,028
  • 4
  • 33
  • 43

5 Answers5

47

Use this one

android:progress="5"

Like "progress", it inherits some of it's attributes from ProgressBar. You find all the attributes at http://developer.android.com/reference/android/widget/SeekBar.html

mseo
  • 3,711
  • 3
  • 19
  • 26
  • I guess that I can do that by this Java code, too right?: seekBar.incrementProgressBy(10); But if I want to increment progress by more then 100, it doesn't work. Do you have any idea? – Adam Jan 10 '12 at 18:18
  • What's your max progress value? Did you call setMax(int max)? I think you should set this to an according value. – mseo Jan 10 '12 at 18:48
25

To get Seekbar at the value of 5

In the xml layout use the max value as 10

  android:max="10"

In the java for seekbar to get at progress 5

 seekbar = (SeekBar) findViewById(R.id.seekBar1);
 seekbar.setProgress(5);

thats it :)

Community
  • 1
  • 1
Danny
  • 1,050
  • 3
  • 11
  • 26
4

I hope this code surely helps you.
Try this...

float discrete=0; 
 float start=0; 
 float end=100; 
 float start_pos=0; 
 int start_position=0; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    start=-10;      //you need to give starting value of SeekBar 
end=10;         //you need to give end value of SeekBar 
    start_pos=5;    //you need to give starting position value of SeekBar 

    start_position=(int) (((start_pos-start)/(end-start))*100); 
    discrete=start_pos; 
    SeekBar seek=(SeekBar) findViewById(R.id.seekBar1); 
    seek.setProgress(start_position); 
    seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 

        @Override 
        public void onStopTrackingTouch(SeekBar seekBar) { 
            // TODO Auto-generated method stub 
            Toast.makeText(getBaseContext(), "discrete = "+String.valueOf(discrete), Toast.LENGTH_SHORT).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 
            // To convert it as discrete value 
            float temp=progress; 
            float dis=end-start; 
            discrete=(start+((temp/100)*dis)); 

        } 
    }); 
} 
Brad Larson
  • 168,330
  • 45
  • 388
  • 563
Balaji Gunasekar
  • 1,323
  • 11
  • 5
1

default value By xml :

android:progress="5"

max value by xml

android:max="10"
Ucdemir
  • 2,070
  • 2
  • 21
  • 35
-1
final Timer timer = new Timer();

seekBar_efficiency.setMax(40);

seekBar_efficiency.setProgress(35);
timer.schedule(new TimerTask() {

    @Override
    public void run() {
        seekBar_efficiency.setProgress(20);
        timer.cancel();
    }
}, 200, 50);
Chuck D
  • 1,537
  • 2
  • 14
  • 29