Creating a SeekBar in Android
Last Updated : 13 Feb, 2025
Android SeekBar is a type of ProgressBar. On touching the thumb on seekbar and dragging it to the right or left, the current value of the progress changes. SeekBar is used for forwarding or backwarding the songs, Video etc. In the setOnSeekBarChangeListener interface is used which provides three methods.
- onProgressChanged: In this method progress is changed and then according to this change the progress value can used in our logic.
- onStartTrackingTouch: In this method when the user has started dragging, then this method will be called automatically.
- onStopTrackingTouch: In this method, when the user stops dragging, then this method will called automatically.
Steps for Implementing SeekBar:
Step 1: Create a new Android Studio project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Working with activity_main.xml
Open your xml file and add a SeekBar and TextView for message as shown below, max attribute in SeekBar define the maximum it can take. Assign ID to SeekBar And TextView.
activity_main.xml:
XML <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" tools:context=".MainActivity"> <TextView android:id="@+id/message_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="32dp" android:text="geeksforgeeks" android:textSize="20sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <SeekBar android:id="@+id/seekbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="150" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Design UI:
Step 3: Working with MainActivity file
Now, open up the activity file and then define the SeekBar and TextView variable, use findViewById() to get the SeekBar and TextView. Performs seek bar change listener event which is used for getting the progress value. By using this event listener we get the value of Progress, and the progress is displayed by using a TextView, which will increase the size.
MainActivity.java package org.geeksforgeeks.demo; import android.os.Bundle; import android.widget.SeekBar; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private SeekBar seekBar; private TextView textMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textMessage = findViewById(R.id.message_id); seekBar = findViewById(R.id.seekbar); // Set the SeekBar change listener seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // Increment 1 in progress and update the text size textMessage.setTextSize(progress + 1); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // Called when the user starts touching the SeekBar } @Override public void onStopTrackingTouch(SeekBar seekBar) { // Called when the user stops touching the SeekBar } }); } }
MainActivity.kt package org.geeksforgeeks.demo import android.os.Bundle import android.widget.SeekBar import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private lateinit var seekBar: SeekBar private lateinit var textMessage: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textMessage = findViewById(R.id.message_id) seekBar = findViewById(R.id.seekbar) // Set the SeekBar change listener seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { // When the progress value has changed override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { // Increment 1 in progress and update the text size textMessage.textSize = (progress + 1).toFloat() } override fun onStartTrackingTouch(seekBar: SeekBar?) { // This method will automatically be called when the user touches the SeekBar } override fun onStopTrackingTouch(seekBar: SeekBar?) { // This method will automatically be called when the user stops touching the SeekBar } }) } }
Output: