Open In App

Dynamic SeekBar in Kotlin

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

SeekBar in Android is a modified version of progressBar that has a draggable thumb in which a user can drag the thumb back and forth to set the current progress value. We can use SeekBar in our Android Devices like Brightness control, volume control etc. It is one of the important user interface elements which provides the option to select the integer values within the defined range like 1 to 100.

In this article, we will learn how to implement a seekbar programmatically in kotlin.

Dynamic-Seek-Bar


Step by Step Implementation

Step 1: Create New Project

To Create a New Project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Modify activity_main.xml file

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

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

</LinearLayout>


Step 3: 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 params and set attributes for that. We will create another variable layout for the main Layout and call from activity_main.xml file using the id main.

val params = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
params.setMargins(100, 0, 100, 0)
seek.layoutParams = params

val layout: LinearLayout = findViewById(R.id.main)

and add the SeekBar named as seek into the linearLayout using

layout.addView(seek)

MainActivity.kt:

Kotlin
package org.geeksforgeeks.demo

import android.os.Bundle
import android.view.ViewGroup
import android.widget.LinearLayout
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)

        // defining SeekBar
        val seek = SeekBar(this)

        // defining parameters for seekBar
        val params = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        params.setMargins(100, 0, 100, 0)
        seek.layoutParams = params

        val layout: LinearLayout = findViewById(R.id.main)

        // Adding SeekBar to main layout
        layout.addView(seek)

        seek.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 touch is started.
                }

                override fun onStopTrackingTouch(seekBar: SeekBar) {
                
                    // add functionalities for when touch is stopped
                    Toast.makeText(this@MainActivity, "SeekBar Progress is: "
                         + seekBar.progress + "%", Toast.LENGTH_SHORT).show()
                }
            })
    }
}

Output:



Next Article

Similar Reads