SeekBar in Android

SourabhSahu 993 views 14 slides Mar 19, 2018
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

SeekBar in android and its attributes


Slide Content

SeekBar
1
Sourabh Sahu

SeekBar

SeekBar is an extension of ProgressBar that
adds a draggable thumb, a user can touch
the thumb and drag left or right to set the
value for current progress.

<SeekBar
android:id="@+id/simpleSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

Methods
SeekBar.OnSeekBarChangeListener is a
listener used as a callback that notifies client
when the progress level of seekbar has been
changed. This listener can be used for both
user initiated changes (in xml file or java class)
as well as for programmatic changes.
seekBarInstanceVariable.setOnSeekBarCh
angeListener(new OnSeekBarChangeListe
ner() {…} – This method is used to notify the
user changes/actions in the SeekBar.

Methods Needs To Be Implemented:
In Seek bar for getting changes in progress we need to implement three
abstract methods. Below is detail description of these three methods:
1. public void onProgressChanged (SeekBar
seekBar, int progresValue, boolean fromUser) {…} –
This listener method will be invoked if any change is made in the SeekBar.
2. public void onStartTrackingTouch(SeekBar seekBar) {…} –
This listener method will be invoked at the start of user’s touch event.
Whenever a user touch the thumb for dragging this method will
automatically called.
3. public void onStopTrackingTouch(SeekBar seekBar) {…} –
This listener method will be invoked at the end of user touch event.
Whenever a user stop dragging the thump this method will be automatically
called.

•getMax():
•We can get the maximum value of the SeekBar
programmatically means in java class. This method returns
an integer value. Below code will get the maximum value
from a Seekbar.
•SeekBar simpleSeekBar = (SeekBar)
findViewById(R.id.simpleSeekBar); // initiate the Seek bar
int maxValue=simpleSeekBar.getMax(); // get maximum
value of the Seek bar getProgress():
•We can get the current progress value from a Seekbar in
java class using getProgress() method. This method
returns an integer value. Below code is used to get the
current progress value from a Seek bar.
•SeekBar
simpleSeekBar=(SeekBar)findViewById(R.id.simpleSeekBar
); // initiate the Seek bar int seekBarValue=
simpleSeekBar.getProgress(); // get progress value from
the Seek bar

 max: max attribute in SeekBar define the
maximum it can take. It must be an integer
value like 10, 20, 100, 200 etc. We can set the
max value in XML file as well as in java class.
By default, a SeekBar takes maximum value of
100.
/*Add in Oncreate() funtion after setContentView()*/
SeekBar simpleSeekBar=(SeekBar)
findViewById(R.id.simpleSeekBar); // initiate the Seekbar
simpleSeekBar.setMax(150); // 150 maximum value for
the Seek bar
<SeekBar android:id="@+id/simpleSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="150"/>


progress: progress is an attribute of
SeekBar used to define the default
progress value, between 0 and max. It
must be an integer value.
• progressDrawable: progress drawable
attribute is used in Android to set the custom
drawable xml for the progress mode of a seekbar.
Progress drawable is used when we need to use a
custom progress of a seekbar.
•Below we set the custom gradient drawable for
the progress mode of a Seekbar.

<SeekBar
android:id="@+id/simpleSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_conte
nt" android:max="200"
android:progress="50"
android:progressDrawable="@drawa
ble/custom_progress"/>


Custom_progress.xml

<?xml version="1.0" encoding="utf-
8"?> <layer-list
xmlns:android="http://schemas.andr
oid.com/apk/res/android" > <item>
<shape> <gradient
android:endColor="#fff"
android:startColor="#f00"
android:useLevel="true" />
</shape> </item> </layer-list>


indeterminate: indeterminate is an
attribute used in android to enable
the indeterminate mode of a
seekbar. In this mode a seekbar
shows a cyclic animation without an
indication of progress. This mode is
used in application when we don’t
know the amount of work has been
done. Here actual progress will be
hidden from user.

<SeekBar
android:id="@+id/simpleSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content
" android:max="200"
android:indeterminate="true"/>
 background: background attribute of seekbar is
used to set the background. We can set a color or
a drawable in the background of a Seekbar.
We can also set the background color in
JAVA class.

<SeekBar
android:id="@+id/simpleSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="200"
android:indeterminate="true"
android:background="#0F0"/>
thumb: thumb attribute is used in seekbar
to draw a thumb on a seekbar. We can use an
image or a drawable for the thumb.

<SeekBar android:id="@+id/simpleSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="200" android:progress="100"
android:thumb="@drawable/thumb"/>

Thank You
14