Android SeekBar


Download Android SeekBar sample

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

Android SeekBar Usage?


Android Switch simple usage in xml layout is given below:

Android SeekBar usage

How to set progress in Android SeekBar?


To set the progress of Android SeekBar set the android:progress as follows:

We can also change the progress of a Android SeekBar programmatically as follows:

How to set max progress value in Android SeekBar?


To set the max progress value of Android SeekBar set the android:max attribute of Android SeekBar as follows:

We can also change the max progress value of a Android SeekBar programmatically as follows:

How to change thumb of Android SeekBar?


Using android:thumb attribute of Android SeekBar we can change the thumb icon of Android SeekBar

How to get progress value of Android SeekBar?


We can get the progress value of an Android SeekBar on runtime as follows:

How to implement Vertical SeekBar?


There are 2 steps to add a vertical seekBar:

  1. Write a class that extends SeekBar, add your logic here to implement vertical seekbar
  2. User this class to define vertical seekbar in your xml layout

Now use it in xml layout as follows:

How to customize Android SeekBar?


We can customize Android SeekBar, by defining an xml under drawable folder as follows:

seekbar.xml

We can use drawable image as Thumb or we can define it in xml under drawable folder as follows:

circle.xml

Now last step of customization of Android SeekBar is to set the android:progressDrawable and android:thumb attribute of Android SeekBar as follows:

How to handle onProgressChanged event of Android SeekBar?


To handle onProgressChanged event of Android SeekBar implement SeekBar.OnSeekBarChangeListener in main activity class, and override 3 methods onProgressChanged, onStartTrackingTouch and onStopTrackingTouch.

Android SeekBar demo

Android SeekBar demo

In this example, We have discussed different aspect of Android Seekbar. Now lets implement the demo of Android SeekBar

Step 1: Create a new project in Android Studio and name it Android SeekBar.

Step 2: Open res -> values ->colors.xml and replace its code with below given code:

colors.xml

Step 3: Open res -> drawable and add a new file seekbar.xml. Replace the code of seekbar.xml with the below given code:

seekbar.xml

Step 4: Open res -> drawable and add a new file circle.xml. Replace the code of circle.xml with the below given code:

circle.xml

Step 5: Open res -> layout -> activity_main.xml and add following code

activity_main.xml

Step 6: Open your MainActivity.java and replace its source with source of my MainActivity.java as follows:

MainActivity.java

Step 7: Clean and run the project to see the result.


Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.