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 xmlns:android="http://schemas.android.com/apk/res/android" 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) { // Write code to perform some action // when progress is changed. } @Override public void onStartTrackingTouch( SeekBar seekBar) { // Write code to perform some action // when touch is started. } @Override public void onStopTrackingTouch( SeekBar seekBar) { // Write code to perform some action // when touch is stopped. 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:
Similar Reads
Creating a SeekBar in Android Android SeekBar is a type of ProgressBar. On touching the thumb on seekbar and dragging it to the right or left, the current value of the progress changes. SeekBar is used for forwarding or backwarding the songs, Video etc. In the setOnSeekBarChangeListener interface is used which provides three met
3 min read
Discrete SeekBar in Android using Library Discrete SeekBar is another most common feature that we can see in most of the apps. We can get to see this Discrete SeekBar in most of the music player apps, Rating apps, or for points given. Discrete SeekBar is one of the advanced systems of giving ratings instead of writing. In the previous artic
3 min read
Creating Custom SeekBar in Android SeekBar can be understood as an extension of ProgressBar in Android. You have to just drag the thumb on SeekBar and drag it towards the backward or forward direction and store the current value of progress changed. SeekBar is widely used in different applications ex - Audio player Video Player etc.
5 min read
Discrete SeekBar in Kotlin In Android Discrete SeekBar is just an advancement of progressBar just like the SeekBar, the only difference in SeekBar and discrete SeekBar being that in discrete SeekBar, we can only set the value only to discrete values like 1, 2, 3, and so on. In this article, we will be discussing how to create
2 min read
Custom Snackbars in Android A Snackbar in Android is a lightweight and quick way to provide feedback to users about an action which is done by the user. It appears as a temporary, dismissible bar at the bottom of the screen and can contain a message and an optional action button which can easily be removed by the user by just
4 min read