Si8
Si8

Reputation: 9235

How to get seekbar default progress position on activity created

I have the following code in onCreate() Activity:

SeekBar vSeekbar = (SeekBar)findViewById(R.id.seekBar1);
final TextView tvText = (TextView) findViewById(R.id.textView);
//sets seekbar tab position from a randomly generated number...
//when the acitivty is first created I would like the SeekBar position to be set in the `textView`
tvText.setText(vSeekbar.getProgress()); //this is not showing anything...

//the onchangelistener works correctly...
vSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
    @Override
    public void onStopTrackingTouch(SeekBar arg0)
    {
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0)
    {
    }

    @Override
    public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
    {
        tvText.setText(String.valueOf(progress)); //this works fine...
    }
});

The seekBar1 position is set by a random number generated when the view is created so no way for me to know beforehand.

How can I get the seekBar1 position when the view is created and set it in the textView.

Upvotes: 0

Views: 646

Answers (2)

Magesh Pandian
Magesh Pandian

Reputation: 9389

After random value set to seekbar call vSeekbar.getProgress() method. before you called then only you got zero value.

vSeekbar.setProgress(randomValue());
tvText.setText(vSeekbar.getProgress()+"");

Upvotes: 2

Mohammad Abbas
Mohammad Abbas

Reputation: 245

You better limit your random values to the ProgressBar maximum value.

For example if your maximum value is 100 then use the following code: vSeekbar.setProgress(randomValue() % 100);

It will limit the random values to 0 to 99

Upvotes: 0

Related Questions