Android SeekBar is the extension of ProgressBar. SeekBar allows the user to change the value using touch event/draggable thumb/left right arrow keys. The user can increase the value by dragging the thumb rightย ย or by pressing the right arrow key. Similarly the user can decrease the value by dragging the thumb left or by pressing the left arrow key.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:max="10"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/seekBar1"
android:layout_marginLeft="29dp"
android:layout_marginTop="14dp" />
</RelativeLayout>
MainActivity.kt
package com.androidian.seekbar
import android.os.Bundle
import android.view.View
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private var seekBar: SeekBar? = null
private var textView: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
seekBar = findViewById<View>(R.id.seekBar1) as SeekBar
textView = findViewById<View>(R.id.textView1) as TextView
// Initialize the textview with '0'
textView!!.text = seekBar!!.progress.toString() + "/" + seekBar!!.max
seekBar!!.setOnSeekBarChangeListener(
object : OnSeekBarChangeListener {
var progress = 0
override fun onProgressChanged(
seekBar: SeekBar,
progresValue: Int, fromUser: Boolean
) {
progress = progresValue
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
// Do something here,
//if you want to do anything at the start of
// touching the seekbar
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
// Display the value in textview
textView!!.text = progress.toString() + "/" + seekBar.max
}
})
}
}