Android Kotlin SeekBar Example

Seekbar in Android | Android SeekBar Example

In this Android SeekBar tutorial we will learn how to use SeekBar in Android Studio. We will be using a SeekBar and show the current progress of the SeekBar in a TextView. So, lets start our Android Seekbar Example using Kotlin.


What is an Android SeekBar?

SeekBar in Android is a widget which has a thumb icon. A user can drag the SeekBar thumb left to right in order to change the current progress of the SeekBar. SeekBar.OnSeekBarChangeListener is a callback that tells the clients that the SeekBar progress has been changed.


Seekbar in Android
Seekbar in Android


Android Kotlin SeekBar Example

1- Start with a new Android Studio Project. Name it “SeekbarExample”. Also, you can give any name to your project.

2- After your project builds successfully you will have two files. “MainActivity.kt” file and “activity_main.xml” file. So first we are going to design our SeekBar in “activity_main.xml” file.




3- Come to your “activity_main.xml” file and then change the default Layout file from Constraint Layout to Relative Layout. Because it is easier to work with Relative Layout.

4- So in our “activity_main.xml” file we will use two TextView and a SeekBar. First TextView will only display the text “SeekBar” while the other TextView will display the current progress of the SeekBar.

5- We add the SeekBar widget with wrap_content width and then wrap_content height. Then we will give a layout_margin of 25dp. The id of our SeekBar is (seekbar) and to display our SeekBar in the center of the screen we are using the attribute layout_centerInParent=”true”.



6- Finally the complete “activity_main.xml” file with two TextView and a SeekBar is like this.



Android Seekbar Example



7- So time to start coding in our “MainActivity.kt” file. So come to your “MainActivity.kt” file. Here we will use setOnChangeListener() method on our SeekBar to handle the changes.


8- In onCreate function we will use seekbar.setOnChangeListener() function and pass the reference of SeekBar.OnSeekBarChangeListener interface. It has three functions:

  • onProgressChanged
  • onStartTrackingTouch
  • onStopTrackingTouch

We use onProgressChanged to show the current progress of the SeekBar in the TextView.



9- So, if you want to display any Toast message when a user drags the SeekBar to left or right then you can set the Toast message in function onStartTrackingTouch and onStopTrackingTouch.



10- So, the complete seekbar.setOnSeekBarChangeListener function will look like this.



11- To show “Progress is : 0%” on the start of the application use this code in the onCreate function.



12- Note that (seekbar) is the id of our SeekBar and (textview) is the id of our TextView that will show the current progress of our SeekBar. Finally, our complete “MainActivity.kt” file will be like this.



So, I hope you understand how to use SeekBar with this Seekbar in Android. But if you still have any queries about this Android Seekbar Example you can ask in the comments section below.





Seekbar in Android
Seekbar in Android


Seekbar in Android



        More Android Examples



Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!