AppCompatSeekBar

In Android, AppCompatSeekBar is an extension of ProgressBar that adds a draggable thumb, a user can touch the thumb and drag left or right to set the value for current progress. As AppCompatSeekBar is advance version of Seekbar, we recommend to user AppCompatSeekBar.

public class AppCompatSeekBar extends SeekBar
java.lang.Object
↳ android.view.View
  ↳ android.widget.ProgressBar
    ↳ android.widget.AbsSeekBar
      ↳ android.widget.SeekBar
        ↳ androidx.appcompat.widget.AppCompatSeekBar

We Recommend to use new UI components i.e. androidx components


Attributes Description
android:max max attribute in SeekBar define the maximum it can take. It must be an integer value like 10, 20, 100, 200 etc. We can set the max value in XML file as well as in java class. By default, a SeekBar takes maximum value of 100.
android:progress progress is an attribute of AppCompatSeekBar used to define the default progress value, between 0 and max, it must be an integer value.
android:progressDrawable progress drawable attribute is used in Android to set the custom drawable xml for the progress mode of a seekbar. Progress drawable is used when we need to use a custom progress of a seekbar.
android:indeterminate It is used to enable the indeterminate progress mode.
android:thumb It is used to set the thumb icon on seekbar to drag left or right.

Create AppCompatSeekBar in xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tvSeekBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0/100"
        android:textSize="12pt"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/seekBar"/>

    <androidx.appcompat.widget.AppCompatSeekBar
        android:id="@+id/seekBar"
        android:progressDrawable="@drawable/seekbar_bg"
        android:layout_width="match_parent"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"/>
</RelativeLayout>
final AppCompatTextView tvProgress = findViewById(R.id.tvSeekBar);

AppCompatSeekBar seekBar = findViewById(R.id.seekBar);
seekBar.setMax(200);
seekBar.setOnSeekBarChangeListener(new AppCompatSeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
        // This listener method will be invoked if any change is made in the SeekBar.
        Toast.makeText(SeekBarActivity.this, "SeekBar: " + seekBar.getProgress(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // This listner method will be invoked at the start of user's touch event. Whenever a user touch the thumb for dragging this method will automatically invoked.
        Toast.makeText(SeekBarActivity.this, "SeekBar Touch Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // This listner method will be invoked at the end of user touch event. Whenever a user stop dragging the thumb this method will automatically invoked.
        tvProgress.setText(seekBar.getProgress() + "/" + seekBar.getMax());
        Toast.makeText(SeekBarActivity.this, "SeekBar Touch Stopped", Toast.LENGTH_SHORT).show();
    }
});


Example screen shot:

Create AppCompatSeekBar Prgramatically as follow:

AppCompatSeekBar seekBar = new AppCompatSeekBar(this);
LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
seekBar.setLayoutParams(params);
// set max progress bar value
seekBar.setMax(200);

seekBar.setOnSeekBarChangeListener(new AppCompatSeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
        // This listener method will be invoked if any change is made in the SeekBar.
        Toast.makeText(SeekBarActivity.this, "SeekBar: " + seekBar.getProgress(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // This listner method will be invoked at the start of user's touch event. Whenever a user touch the thumb for dragging this method will automatically invoked.
        Toast.makeText(SeekBarActivity.this, "SeekBar Touch Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // This listner method will be invoked at the end of user touch event. Whenever a user stop dragging the thumb this method will automatically invoked.
        tvProgress.setText(seekBar.getProgress() + "/" + seekBar.getMax());
        Toast.makeText(SeekBarActivity.this, "SeekBar Touch Stopped", Toast.LENGTH_SHORT).show();
    }
});

Below code show you how we can set custom android:progressDrawable using xml, first create a xml file name is seekbar_bg.xml in drawable folder like below:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <gradient
                android:endColor="@android:color/holo_blue_light"
                android:startColor="@android:color/holo_orange_dark"
                android:useLevel="true" />
        </shape>
    </item>
</layer-list>

Set background of AppCompatSeekBar in xml as:

<androidx.appcompat.widget.AppCompatSeekBar
    android:id="@+id/seekBar"
    android:progressDrawable="@drawable/seekbar_bg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>