Open In App

Discrete SeekBar in Kotlin

Last Updated : 26 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 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:elevationSets 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: 

dir


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
<?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:

discrete-seekbar


Step 3: Create SeekBar in MainActivity.kt file

Open app > src > main > java > {package-name} > MainActivity.kt and do the following changes: 

MainActivity.kt:

Kotlin
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()
            }
        })
    }
}

Output:


Next Article

Similar Reads