Android tutorial - Android seekbar | Seekbar Android | Android SeekBar Example - android app development - android studio - android development tutorial
Learn android - android tutorial - Android seekbar - android examples - android programs
What is Android SeekBar?
- A 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 or use the arrow keys.
- Placing focusable widgets to the left or right of a SeekBar is discouraged.
- In Android, 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 is one of the very useful user interface element in Android that allows the selection of integer values using a natural user interface.
Methods Needs To Be Implemented:
- We need to implement three abstract methods. Below is the detailed description of these three methods:
- This listener method will be invoked if any change is made in the 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.
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.
Attributes of SeekBar In Android:
- Now let’s we discuss important attributes that helps us to configure a SeekBar in xml file (layout).
1. id:
- id attribute uniquely identify SeekBar.
2. 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.
- Below we set 150 maximum value for a Seek bar.
Setting max value of SeekBar Progress In Java Class :
3. progress:
- progress is an attribute of SeekBar used to define the default progress value, between 0 and max. It must be an integer value.
- Below we set the 200 max value and then set 50 default progress value.
Setting progress In Java Class :
4. 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.
- Step 1: Add this code in activity_main.xml or main.xml
- Step 2: Create a new drawable resource xml in drawable folder and name it custom_progress. Here add the below code which creates gradient effect in seekbar.
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.
- Below we set the green color for the background of a Seek bar.
padding:
- padding attribute of seekbar is used to set the padding from left, right, top or bottom.
- paddingRight: padding right attribute is used to set the padding from the right side of the Seekbar.
- paddingLeft: padding left attribute is used to set set the padding from the left side of the Seekbar.
- paddingTop: padding top attribute is used to set the padding from the top side of the Seekbar.
- paddingBottom: padding bottom attribute is used to set the padding from the bottom side of the Seek bar.
- Padding: padding attribute is used to set the padding from the all side’s of the Seekbar.
- Below we set the 20dp padding from the top of the Seek bar.
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.
- Below is an example code in which we set a drawable icon for the thumb of the seekbar.
- First download the thumb icon and save in drawable folder of your project. You can also click on below icon and then download it:
SeekBar Example In Android Studio:
- Step 1: Create a new project and name it SeekBarExample
- Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code:
- In this step we open an xml file and add the code for displaying a seekbar by using its different attributes like max, default progress and few more.
- Step 3: Open src -> package -> MainActivity.java
- In this step we open MainActivity and add the code to initiate the seekbar and then perform seekbar changed listener event for getting the changes in the progress of the seekbar. By using this event listener we set get the current value of a seekbar and when a user stop the tracking touch, the value of progress is displayed by using a Toast.
Example 2:
- Step 1: Create a new project and name it SeekBarExample
- Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code:
- In this step we open xml file and add the code for displaying a vertical seekbar by using its different attributes like max, default progress etc. In this xml for displaying a vertical seekbar we used a rotational attribute and set 270 value for that.
- Step 3: Create an xml file in drawable -> custom_progress.xml
- In this step we create a custom drawable xml for the seek bar. In this xml we create a layer list in which we create an item and then set the gradient colors for our custom seek bar.
- Step 4: Open src -> package -> MainActivity.java
- open MainActivity and here we add the code to initiate the vertical seekbar
- perform seek bar changed listener event for getting the changes in the progress of the seek bar.
- using event listener we get the current progress value of a seek bar and when a user stop the tracking touch, that value of progress is displayed by using a Toast.