Open In App
Related Articles

Dynamic SeekBar in Kotlin

Improve Article
Save Article
Like Article

Android seekBar is an modified version of progressBar that have draggable thumb in which a user can drag the thumb back and forth to set current progress value. We can use seekbar in our android device like Brightness control, volume control etc.

It is one of the important user Interface element which provides the option to select the integer values within the defined range like 1 to 100.

By dragging the thumb in SeekBar, we can slide back and forth to choose a value between minimum and maximum integer value which we defined using android:min and android:max attributes respectively.

First we create a new project by following the below steps:

  1. Click on File, then New => New Project.
  2. After that include the Kotlin support and click on next.
  3. Select the minimum SDK as per convenience and click next button.
  4. Then select the Empty activity => next => finish.

Here, we create SeekBar programmatically in Kotlin file.

Use activity_main.xml file

First of all, use LinearLayout and set its attributes like id, layout_width , context etc.


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

Create SeekBar in MainActivity.kt file

Here, we need to declare seek to create SeekBar like this:

 val seek = SeekBar(this)

then, we create another variable lParams and set attributes for that. We will create another variable lLayout for LinearLayout and call from activity_main.xml file using the id container.

val lLayout = findViewById(
        // Adding SeekBar to LinearLayout

and add the SeekBar named as seek into the linearLayout using



package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.SeekBar
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
       // defining SeekBar
        val seek = SeekBar(this)
        val lParams = LinearLayout.LayoutParams(
        lParams.setMargins(50, 50, 50, 50)
        seek.layoutParams = lParams
        val lLayout = findViewById<LinearLayout>(
        // Adding SeekBar to LinearLayout
            object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(
                seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                // write custom code when progress is changed
            override fun onStartTrackingTouch(seekBar: SeekBar) {
                // write custom code when touch is started.
            override fun onStopTrackingTouch(seekBar: SeekBar) {
                // write custom code when touch is stopped
                    "SeekBar Progress is: " + seekBar.progress + "%",

AndroidManifest.xml file


    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

Run as Emulator:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Mar, 2022
Like Article
Save Article
Similar Reads