0

The maximum value of SeekBar is 100 then How to set a Seekbar values default as 50 (starting at 50%). The seek bar will be start at 50.

I got a sample code but it will start at 0 to 100. Please answer me if you have any idea... Thank you.

     public class MainActivity extends Activity implements OnSeekBarChangeListener{
     private SeekBar bar; // declare seekbar object variable
     // declare text label objects
     private TextView textProgress,textAction;
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
    super.onCreate(savedInstanceState);
    // load the layout
    setContentView(R.layout.activity_main);     
    bar = (SeekBar)findViewById(R.id.seekBar1); // make seekbar object
    bar.setOnSeekBarChangeListener(this); // set seekbar listener.
    // since we are using this class as the listener the class is "this"

    // make text label for progress value
    textProgress = (TextView)findViewById(R.id.textViewProgress);
    // make text label for action
    textAction = (TextView)findViewById(R.id.textViewAction);

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {
    // TODO Auto-generated method stub

    // change progress text label with current seekbar value
    textProgress.setText("The value is: "+progress);
    // change action text label to changing
    textAction.setText("changing");
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    textAction.setText("starting to track touch");

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    seekBar.setSecondaryProgress(seekBar.getProgress());
    textAction.setText("ended tracking touch");     
    }

     }
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
krishnan muthiah pillai
  • 2,711
  • 2
  • 29
  • 35
  • 1
    possible duplicate of [How to set seekbar min and max value](http://stackoverflow.com/questions/20762001/how-to-set-seekbar-min-and-max-value) – Himanshu Agarwal Oct 16 '14 at 08:31

3 Answers3

22

in XML :

android:progress="50" // or any other value 

in Java :

mSeekbar.setProgress(50); // or any other value 

and if you don't know what's the Max Value and just want to make it 50%:

mSeekbar.setProgress(mSeekbar.getMax()/2);
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
2

See this and here is how you can do

 seekBar.setMax(0);
 seekBar.setMax(100);
 seekBar.setProgress(50);

in xml you can do like this android:progress="50"

Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47
0

Set the MaxValue once before you set the progress value . In that way the system knows what proportion of the seekbar has to be colored.

shellym
  • 546
  • 1
  • 5
  • 11