How to display some value on seekbar as default?

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.

Answers


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


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


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

        } 
    }); 
} 

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

Need Your Help

adapter-Any real example of Adapter Pattern

oop design-patterns adapter software-design

I want to demonstrate use of Adapter Pattern to my team. I've read many books and articles online. Everyone is citing an example which are useful to understand the concept (Shape, Memory Card, Elec...

Java equivalent to #region in C#

java c# eclipse folding

I want to use regions for code folding in Eclipse; how can that be done in Java?