Android | Creating a SeekBar - GeeksforGeeks

30 Nov, 2021
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.
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.
Below are the steps for Creating SeekBar Android Application:
Step1: Create a new project. After that, you will have java and XML file.
Step2: 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.
Step3: Now, open up the activity java file and then define the SeekBar and TextView variable, use findViewById() to get the SeekBar and TextView.
Step4: 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.
Step5: Now run the app and touch the thumb and then Drag it, the Text size will increase automatically.
The complete code of MainActivity.java or activity_main.xml of SeekBar is given below:
activity_main.xml
MainActivity.java
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     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"    tools:context=".MainActivity">      <TextView        android:id="@+id/message_id"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:text="geeksforgeeks"        android:textStyle="bold"        android:textSize="20sp"        android:layout_gravity="center"/>    <SeekBar        android:id="@+id/seekbar"        android:layout_marginTop="400dp"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:max="150"/>  </RelativeLayout>
package org.geeksforgeeks.navedmalik.seekbar;  // Import the librariesimport android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.SeekBar;import android.widget.TextView;  public class MainActivity extends AppCompatActivity {      // Define the global variable    SeekBar seekbar;    TextView Text_message;      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);          // Binding the layout to the file        setContentView(R.layout.activity_main);          // use findViewById() to get the Button        Text_message            = (TextView)findViewById(R.id.message_id);        seekbar            = (SeekBar)findViewById(R.id.seekbar);          // Get the progress value of the SeekBar        // using setOnSeekBarChangeListener() method        seekbar            .setOnSeekBarChangeListener(                new SeekBar                    .OnSeekBarChangeListener() {                          // When the progress value has changed                        @Override                        public void onProgressChanged(                            SeekBar seekBar,                            int progress,                            boolean fromUser)                        {                              // increment 1 in progress and                            // increase the textsize                            // with the value of progress                            message.setTextSize(progress + 1);                        }                          @Override                        public void onStartTrackingTouch(SeekBar seekBar)                        {                              // This method will automatically                            // called when the user touches the SeekBar                        }                          @Override                        public void onStopTrackingTouch(SeekBar seekBar)                        {                              // This method will automatically                            // called when the user                            // stops touching the SeekBar                        }Prograss                    });    }}
vartika02
kalrap615
Android-Bars
Android
Java
Java
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
CardView in Android With Example
Services in Android with Example
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Stream In Java
Object Oriented Programming (OOPs) Concept in Java