Discrete Seekbar in Android
Last Updated :
17 Jul, 2022
Android discrete SeekBar is an extension of ProgressBar that has a draggable thumb. Users can use the thumb back and forth to set the current progress of the SeekBar. Discrete SeekBar works for discrete values.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language. Now we will work on our main layout file and add the default SeekBar to the layout. Please note here we won’t use any third-party library to make the discrete SeekBar.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:gravity = "center"
android:orientation = "vertical" >
< SeekBar
android:id = "@+id/seekBar"
style = "@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:max = "5"
android:progress = "2" />
</ LinearLayout >
|
In the activity_main.xml file, we have used the android discrete SeekBar widget. Note that we have used style=”@style/Widget.AppCompat.SeekBar.Discrete” style to use SeekBar widget as discrete SeekBar. Also, we have set the maximum value for SeekBar to 5. Attribute android:progress=”2″ is responsible for setting the current progress of the discrete SeekBar. Now, we will access this SeekBar widget in the Java file.
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. In this file, we will access the discrete SeekBar Widget. Add this below code in your MainActivity.java to perform actions.
Java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SeekBar seekBar = findViewById(R.id.seekBar);
if (seekBar != null ) {
seekBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(
SeekBar seekBar, int progress,
boolean fromUser)
{
}
@Override
public void onStartTrackingTouch(
SeekBar seekBar)
{
}
@Override
public void onStopTrackingTouch(
SeekBar seekBar)
{
Toast
.makeText(
MainActivity. this ,
"Current value is "
+ seekBar.getProgress(),
Toast.LENGTH_SHORT)
.show();
}
});
}
}
}
|
In the MainActivity.java file, we have accessed the SeekBar widget. Then, we set the listener, seekBarChangeListener, to show a Toast message that displays the current progress of the discrete SeekBar. We have completed all the steps. Now run your app and see the output of the app.
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...