Android Tutorial

(avery) #1
Android Tutorial 193

<SeekBar
android:id=”@+id/seekbar1”
android:layout_height=”wrap_content”
android:layout_width=”240px”
android:max=”500” />


With this sample SeekBar, the user can drag the thumb to any
value between 0 and 500. Although this is shown visually, it might
be useful to show the user what exact value the user is selecting.
To do this, you can provide an implementation of the
onProgressChanged() method, as shown here:

SeekBar seek = (SeekBar) findViewById(R.id.seekbar1);
seek.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(
SeekBar seekBar, int progress,boolean fromTouch) {
((TextView)findViewById(R.id.seek_text))
.setText(“Value: “+progress);
seekBar.setSecondaryProgress(
(progress+seekBar.getMax())/2);
}
});


There are two interesting things to notice in this example. The first
is that the fromTouch parameter tells the code if the change came
from the user input or if, instead, it came from a programmatic
change as demonstrated with the regular ProgressBar controls. The
second interesting thing is that the SeekBar still enables you to set
a secondary progress value. In this example, we set the secondary
indicator to be halfway between the user’s selected value and the
maximum value of the progress bar. You might use this feature to
show the progress of a video and the buffer stream.
Free download pdf