Dynamic SeekBar in Kotlin
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:
- Click on File, then New => New Project.
- After that include the Kotlin support and click on next.
- Select the minimum SDK as per convenience and click next button.
- 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.
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