Open In App
Related Articles

Discrete SeekBar in Kotlin

Like Article
Save Article
Report issue

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. 

Different Attributes of Android Discrete SeekBar

XML AttributesDescription
android:maxSets the maximum value
android:minSets the minimum value
android:progressSpecifies the already set progress value
android:progressDrawableSets drawable of the progress mode.
android:thumbHelps to draw a thumb on seekBar..
android:thumbTintSet blending mode to apply the thumb tint.
android:thumbTintModeSet tint to apply on tick mark drawable.
android:tickMarkTintSet blending mode used to apply the tick mark tint.
android:tickMarkTintModeSet blending mode used to apply the tick mark tint.
android:backgroundSets background of the view
android:idSets unique id of the view
android:elevationSets base z-depth of the view

First step is to create a new Project in Android Studio. For this follow these steps:

  • Click on File, then New and then New Project, and give name whatever you like
  • Then, select Kotlin language Support and click next button.
  • Select minimum SDK, whatever you need
  • Select Empty activity and then click finish.

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: 

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: 

Modify activity_main.xml file


<?xml version="1.0" encoding="utf-8"?>




This style is used to display the seekBar to make it work for discrete values.

Create SeekBar in MainActivity.kt file

Open app/src/main/java/yourPackageName/MainActivity.kt and do the following changes: 


package com.geeksforgeeks.myfirstKotlinapp
import android.os.Bundle
import android.widget.SeekBar
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        //accessing the seekbar from our layout
        val seekBar = findViewById<SeekBar>(
        seekBar?.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
               //here we can write some code to do something when progress is changed
            override fun onStartTrackingTouch(seekBar: SeekBar) {
               //here we can write some code to do something whenever the user touche the seekbar
            override fun onStopTrackingTouch(seekBar: SeekBar) {
                // show some message after user stopped scrolling the seekbar
                Toast.makeText(this@MainActivity, "Discrete Value of SeekBar is  " + seekBar.progress, Toast.LENGTH_SHORT).show()


AndroidManifest.xml file


<?xml version="1.0" encoding="utf-8"?>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>


Run as Emulator:

Last Updated : 28 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads