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 a SeekBar in Kotlin.
Important Attributes of Discrete SeekBar
| XML Attributes | Description |
|---|---|
| android:max | Sets the maximum value |
| android:min | Sets the minimum value |
| android:progress | Specifies the already set progress value |
| android:progressDrawable | Sets drawable of the progress mode. |
| android:thumb | Helps to draw a thumb on seekBar.. |
| android:thumbTint | Set blending mode to apply the thumb tint. |
| android:thumbTintMode | Set tint to apply on tick mark drawable. |
| android:tickMarkTint | Set blending mode used to apply the tick mark tint. |
| android:tickMarkTintMode | Set blending mode used to apply the tick mark tint. |
| android:elevation | Sets base z-depth of the view |
Step by Step Implementation
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. After doing this you will see some directories on the left hand side after your project/gradle is finished loading. It should look like this:

Step 2: Modify activity_main.xml file
After that, we need to design our layout. For that we need to work with the XML file. Go to app > res > layout and paste the following code.
style=”@style/Widget.AppCompat.SeekBar.Discrete”This style is used to display the seekBar to make it work for discrete values.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/main"
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SeekBar
android:id="@+id/seek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:max="10"
android:min="1"
android:progress="7"
android:layout_margin="40dp"/>
</LinearLayout>
Design UI:

Step 3: Create SeekBar in MainActivity.kt file
Open app > src > main > java > {package-name} > MainActivity.kt and do the following changes:
MainActivity.kt:
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.SeekBar
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//accessing the seekbar from our layout
val seekBar: SeekBar = findViewById(R.id.seek)
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
// add functionalities for when progress is changed
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
// add functionalities for when the user starts touching the seekbar
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
// add functionalities for when the user stops touching the seekbar
Toast.makeText(this@MainActivity, "Value: " + seekBar.progress, Toast.LENGTH_SHORT).show()
}
})
}
}