How to Use Seekbar in Android Activity
What is a Seekbar?
Seekbar is like a progress bar or you can say it’s an extension of progress bar with a thumb attached.
Please follow the steps below in order to use seekbar in android activity:
Step 1) First of all, go to your palette, search for a widget called seekbar and just drag and drop into seekbar widget in your activity.
Step 2) We can also change the property of the seekbar so that its width is adjusted to the width of your layouts so go to the property called layout_width or layout : width and instead of wrap content, you can change this width, layout width to fill parent and it will fill the parent or the layout.
Step 3) Now the second thing we are going to do is to take a large text box and so that we can show the progress of the seekbar or what’s happening in the seekbar.
Step 4) Now let’s go to our mainactivity.java and let’s write some code to our mainactivity.java file and declare two variables, one is SeekBar and the other one is TextView.
Step 5) Now create a method which we will be called in our onCreate method and cast our seekbar and text view variables in side our newly created method.
Step 6) Now before adding the listener to this seekbar let’s set some text on our text view so that it can print the progress of the seekbar.
Step 7) Now we are going to take our seekbar and add our listener to this seekbar so take the seek_bar.setseekbarchangelistener and in here, we will add a new SeekBarChangeListener(). So new OnSeekbarChangeListener() and you will notice that whenever you press enter, it adds three methods to your OnSeekbarChangeListener() – one is onProgressChangedd, other is onStartTrackingTouch and third is ononStopTrackingTouch. First is for tracking the progress of your seekbar, second is on tracking the start of your seekbar and the third one is on tracking the stop of your seekbar.
Step 8) So now inside this OnSeekbarChangeListener() we have to create a variable such as int progress value, progress_value which will be used to store the progress of the seekbar.
Step 9) Now we are going to go to this method called onProgressChanged method and in here, you can see a variable called progress and this will give you the progress of the seekbar. So we will assign progress to our user defined variable which is progress value. And now we will assign this progress to the text view. So what we are going to do here, we are going to copy the same text which we have set in the text view and instead of this getProgress, we will just pass the progress variable here. So this will show us the progress of our seekbar in the text view.
Step 10) The same we are going to do in the onStopTrackingTouch because whenever this seekbar process or seekbar drag is stopped, we also want to, you know, show this value of the text box but this is stored in the progress_value which is global variable for these three methods so change it to the progress_value.
Step 11) We can also show some text message using toast OK so we will use toast.maketext which takes three argument – first is your class name or your context(MainActivity.this) Second is the message, for example, "seekbar in progress" and the third argument, toast.length_long
Step 12) The same messages we can add to our other two method onStartTrackingTouch and onStopTrackingTouch.
Step 13) Now our method is done so we just need to call this method inside our onCreate method in here and then we just need to run our program.
So now our program is running and you will see the initial status of our seekbar is zero and the maximum value of our seekbar is 100. So because it’s on the left, top left and of the, you know, seekbar that’s why our progress is zero right now against 100. Now let’s try to drag this thumb towards the right and let’s see what happens. and you will see it changes, this value which is covered and it changes the message or toast in here. So the process changes and the message here also changes.