Skip to content
Related Articles

Related Articles

Improve Article
Discrete SeekBar in Kotlin
  • Last Updated : 23 Feb, 2021

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 folllowing code:

Modify activity_main.xml file




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
  
    <SeekBar
            android:id="@+id/seekBar"
            style="@style/Widget.AppCompat.SeekBar.Discrete"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:min="1"
            android:max="10"
            android:progress="7"/>
  
</RelativeLayout>

Note:

style=”@style/Widget.AppCompat.SeekBar.Discrete”

. 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 androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.SeekBar
import android.widget.Toast
   
class MainActivity : AppCompatActivity() {
   
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
   
        //accesing the seekbar from our layout
        val seekBar = findViewById<SeekBar>(R.id.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"?>
          package="i.apps.myapplication">
  
    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
  
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
  
</manifest>

Run as Emulator:



Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!



My Personal Notes arrow_drop_up
Recommended Articles
Page :