SeekBar in Kotlin - GeeksforGeeks

28 Mar, 2022
Android seekBar is a 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.
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 will add the Seekbar widget in LinearLayout and set its attributes like id, margin etc.
Kotlin
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:orientation="vertical">      <SeekBar        android:id="@+id/seekBar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="50dp"        android:max="100"        android:min="0" /></LinearLayout>
Name of the application specify in the strings.xml file
Kotlin
<resources>    <string name="app_name">SeekBarInKotlin</string></resources>
In the file, we first declare a variable seek and call the seekbar from the xml file using the id.
val seek = findViewById(R.id.seekBar)
then, setOnClickListener to perform some action on the seekBar.
seek?.setOnSeekBarChangeListener
and display the toast message using
Toast.makeText(this@MainActivity,
   "Progress is: " + seek.progress + "%",
    Toast.LENGTH_SHORT).show()

Kotlin
package com.geeksforgeeks.myfirstkotlinapp  import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.SeekBarimport android.widget.Toast  class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          val seek = findViewById<SeekBar>(R.id.seekBar)        seek?.setOnSeekBarChangeListener(object :            SeekBar.OnSeekBarChangeListener {        override fun onProgressChanged(seek: SeekBar,              progress: Int, fromUser: Boolean) {          // write custom code for progress is changed        }          override fun onStartTrackingTouch(seek: SeekBar) {            // write custom code for progress is started        }           override fun onStopTrackingTouch(seek: SeekBar) {                // write custom code for progress is stopped          Toast.makeText(this@MainActivity,               "Progress is: " + seek.progress + "%",                Toast.LENGTH_SHORT).show()            }        })    }}
XML
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"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>
 
ayushpandey3july
Android-Bars
Kotlin Android
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Resource Raw Folder in Android Studio
Services in Android with Example
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Android UI Layouts
Services in Android with Example
Android RecyclerView in Kotlin
Content Providers in Android with Example