SeekBar Android

To display a ProgressBar with a draggable thumb, such that the end-user can drag the thumb left and right to move the progress of an activity, the SeekBar is used in Android. The methods to perform an event handling for seek bar is facilitated by the SeekBar.OnSeekBarChangeListener interface. The AbsSeekBar class has the Android SeekBar and RatingBar classes as subclasses.

Android SeekBar Example:

activity_main.xml:

In the activity_main.xml file, we will drag the seek bar from the pallet.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/seekBar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginEnd="8dp"
       android:layout_marginStart="8dp"
       android:layout_marginTop="372dp"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to display the seek bar and to perform the event handling.

package com.example.radioapp;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
   SeekBar seekBar;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
 
       seekBar=(SeekBar)findViewById(R.id.seekBar);
       seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
           @Override
           public void onProgressChanged(SeekBar seekBar, int progress,
                                         boolean fromUser) {
               Toast.makeText(getApplicationContext(),"Progress: "+progress, Toast.LENGTH_LONG).show();
           }
 
           @Override
           public void onStartTrackingTouch(SeekBar seekBar) {
               Toast.makeText(getApplicationContext(),"Started!!", Toast.LENGTH_LONG).show();
           }
 
           @Override
           public void onStopTrackingTouch(SeekBar seekBar) {
               Toast.makeText(getApplicationContext(),"Stopped!!", Toast.LENGTH_LONG).show();
           }
       });
   }
}

Output 1: Displaying the SeekBar.

Output 2: Displaying the Progress.

Output 3: When Progress Stopped.

Please follow and like us:
Content Protection by DMCA.com