Open In App

Discrete SeekBar in Kotlin

Last Updated : 28 Oct, 2021
Like Article

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 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:background Sets background of the view
android:id Sets unique id of the view
android:elevation Sets 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:

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads