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 Switch simple usage in xml layout is given below:
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:
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:
Using android:thumb attribute of Android SeekBar we can change the thumb icon of Android SeekBar
We can get the progress value of an Android SeekBar on runtime as follows:
There are 2 steps to add a vertical seekBar:
Now use it in xml layout as follows:
We can customize Android SeekBar, by defining an xml under drawable folder as follows:
We can use drawable image as Thumb or we can define it in xml under drawable folder as follows:
Now last step of customization of Android SeekBar is to set the android:progressDrawable and android:thumb attribute of Android SeekBar as follows:
To handle onProgressChanged event of Android SeekBar implement SeekBar.OnSeekBarChangeListener in main activity class, and override 3 methods onProgressChanged, onStartTrackingTouch and onStopTrackingTouch.
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:
Step 3: Open res -> drawable and add a new file seekbar.xml. Replace the code of seekbar.xml with the below given code:
Step 4: Open res -> drawable and add a new file circle.xml. Replace the code of circle.xml with the below given code:
Step 5: Open res -> layout -> activity_main.xml and add following code
Step 6: Open your MainActivity.java and replace its source with source of my MainActivity.java as follows:
Step 7: Clean and run the project to see the result.