Discrete SeekBar in Android using Library - GeeksforGeeks

23 Feb, 2021
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 article, we have implemented Discrete SeekBar in Android without using any 3rd party library. But, in this article, we are going to see how to implement Discrete SeekBar in the Android application. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 
Discrete SeekBar is used in most music player apps.
It is mostly used for giving a rating in most e-commerce apps.
Using Discrete SeekBar in the application improves user experience.
Step 1: Create a New Project
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.
Step 2: Add dependency of Discrete SeekBar library in build.gradle file
Then Navigate to Gradle scripts and then to build.gradle(Module) level. Add below line in the build.gradle file in the dependencies section.
implementation ‘org.adw.library:discrete-seekbar:1.0.1’
now click on Sync now it will sync your all files in build.gradle().
Step 3: Create a new Discrete SeekBar in your activity_main.xml file
Navigate to the app > res > layout to open the activity_main.xml file. Below is the code for the activity_main.xml file. 
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingLeft="16dp"    android:paddingTop="16dp"    android:paddingRight="16dp"    android:paddingBottom="16dp"    tools:context=".MainActivity">      <!--Discrete SeekBAr-->    <org.adw.library.widgets.discreteseekbar.DiscreteSeekBar        android:id="@+id/seekBar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        app:dsb_indicatorColor="@color/purple_200"        app:dsb_max="100"        app:dsb_min="0"        app:dsb_progressColor="@color/purple_200"        app:dsb_rippleColor="@color/purple_200"        app:dsb_trackColor="@color/purple_200" />      </RelativeLayout>
Step 4: 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. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;import org.adw.library.widgets.discreteseekbar.DiscreteSeekBar;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // Discrete SeekBar        DiscreteSeekBar discreteSeekBar = (DiscreteSeekBar) findViewById(R.id.seekBar);        discreteSeekBar.setProgress(80);    }}
Now click on the run option it will take some time to build Gradle. After that, you will get output on your device as given below.
Android-Bars
Technical Scripter 2020
Android
Java
Technical Scripter
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different Ways to Change the Project Name in Android Studio
Difference Between Implicit Intent and Explicit Intent in Android
Android SDK and it's Components
Fix “SDK tools directory is missing” Error in Android Studio
What is Context in Android?
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples