Android Studio Tutorial SeekBar

SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch thumb and drag left or right to set the current progress level or various other task.

In this example, the seekbar will be used to display a percentage. As the user moves the SeekBar left to right the percentage value being displayed will change from 0 to 100.

User interaction is captured using the SeekBar.OnSeekBarChangeListener

The SeekBar.OnSeekBarChangeListener has three methods :

onProgressChanged – Notification that the progress level has changed.

onStartTrackingTouch – Notification that the user has started a touch gesture.

onStopTrackingTouch – Notification that the user has finished a touch gesture.

Step 1 :

Create a new Android Studio project

Step 2 :

In the XML file select the SeekBar from the Widgets palette. Also add a TextView, this will be used to display the percent.

Step 3 :

open up the java file and then define the SeekBar and TextView variable, use findViewById() to get the SeekBar and TextView.

Step 4 :

Use the SeekBar.OnSeekBarChangeListener event and use the method OnProgressChanged to capture the change in progress

Step 5 :

Run app, as the SeekBar is dragged from left to right the percentage should change in the TextView

Example Source Code

MainActivity .java

public class MainActivity extends AppCompatActivity {

    NumberFormat percentFormat = NumberFormat.getPercentInstance();// percent format
    TextView percentTextView; // Shows percent
    double percent = 0; // setting initial percentage


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        percentTextView = (TextView) findViewById(R.id.percentTextView);

        SeekBar percentSeekBar = (SeekBar) findViewById(R.id.percentSeekBar);
        percentSeekBar.setOnSeekBarChangeListener(seekBarListener);

    }
    void calc() {
        percentTextView.setText(percentFormat.format(percent));
    }
    final SeekBar.OnSeekBarChangeListener seekBarListener =
            new SeekBar.OnSeekBarChangeListener() {
                // update percent, then call calculate
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                                              boolean fromUser) {
                    percent = progress / 100.0;// set percent based on progress
                    calc();
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                }
            };
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <SeekBar
        android:id="@+id/percentSeekBar"
        android:layout_width="186dp"
        android:layout_height="24dp"
        android:layout_marginStart="201dp"
        android:layout_marginTop="357dp"
        android:layout_marginEnd="169dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/percentTextView"
        android:layout_width="79dp"
        android:layout_height="29dp"
        android:layout_marginStart="166dp"
        android:layout_marginEnd="166dp"
        android:layout_marginBottom="80dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/percentSeekBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Check Out Latest Tutorials Here!

Android Studio Loading Animation Between Activities

Progress Dialog is dialog showing a progress indicator and an optional text message or view. The methods of Progress Dialog being used in this tutorial are: ProgressDialog.setTitle() – Used to set title of dialog box ProgressDialog.setMessage() – Used to set dialog message being displayed ProgressDialog.setProgressStyle() – Choose the style of indicator ProgressDialog.dismiss() – Dismiss the … Read more Android Studio Loading Animation Between Activities

Python chr()

The chr() function returns a character that represents the specified unicode. Syntax chr(i) The chr() method takes only one integer as argument. The range may vary from 0 to 1,1141,111(0x10FFFF in base 16). The chr() method returns a character whose unicode point is num, an integer. If an integer is passed that is outside the range then … Read more Python chr()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s