Skip to content
Related Articles

Related Articles

Improve Article

Dynamic SeekBar in Kotlin

  • Last Updated : 23 Feb, 2021

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"?>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
  
</LinearLayout>

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(R.id.container)
        // Adding SeekBar to LinearLayout
        lLayout?.addView(seek)

and add the SeekBar named as seek into the linearLayout using

lLayout?.addView(seek)




package com.geeksforgeeks.myfirstkotlinapp
  
import androidx.appcompat.app.AppCompatActivity
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?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
       // defining SeekBar
        val seek = SeekBar(this)
        val lParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        lParams.setMargins(50, 50, 50, 50)
        seek.layoutParams = lParams
  
        val lLayout = findViewById<LinearLayout>(R.id.container)
        // Adding SeekBar to LinearLayout
        lLayout?.addView(seek)
  
        seek.setOnSeekBarChangeListener(
            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
                Toast.makeText(this@MainActivity,
                    "SeekBar Progress is: " + seekBar.progress + "%",
                    Toast.LENGTH_SHORT).show()
            }
        })
    }
}

AndroidManifest.xml file




package="com.geeksforgeeks.myfirstkotlinapp">
  
<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 :