Discrete SeekBar in Kotlin

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

Modify activity_main.xml file

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


AndroidManifest.xml file

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Run as Emulator:





My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.