Android Seekbar Example

Share on Google+1Tweet about this on Twitter5Share on Facebook11Share on LinkedIn1

This tutorial will help you understand android SeekBar. Here in this example, we’ll see how to create and customize SeekBar with different color and layout.

SeekBar is one of the useful user interface element in android applications. SeekBar is an extension of ProgressBar that allows the selection of integer values using a natural user interface. Basically SeekBar has a thumb that you can slide to choose a value between 0 and some maximum that you set. A typical usage of Seekbar is your device brightness control and volume control.

We can add a SeekBar widget using element. android:max property is basically used to set a maximum integer value for selection using SeekBar. android:progress property is basically used to set a integer value for SeekBar progress.

Android Seekbar Example

activity_seekbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#F0F0F5" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/background_view_rounded_single"
        android:orientation="vertical"
        android:visibility="visible" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="Seekbar"
            android:textColor="#ffffff"
            android:textSize="20sp" />

        <SeekBar
            android:id="@+id/seek1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:progress="20"
            android:secondaryProgress="20" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="Custom Seekbar"
            android:textColor="#fcfcfc"
            android:textSize="20sp" />

        <SeekBar
            android:id="@+id/volume_bar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:max="100"
            android:progress="0"
            android:progressDrawable="@drawable/progressbar"
            android:secondaryProgress="0" />
    </LinearLayout>

</RelativeLayout>

background_view_rounded_single.xml

<?xml version="1.0" encoding="UTF-8"?>
<inset android:insetLeft="1.0px" android:insetRight="1.0px" android:insetTop="0.0px" android:insetBottom="1.0px"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <selector>
        <item android:state_pressed="true">
            <shape>
                <gradient android:startColor="@color/rounded_container_bg" android:endColor="@color/rounded_container_bg" android:angle="270.0" />
                <corners android:radius="11.0dip" />
            </shape>
        </item>
        <item>
            <shape>
            	<stroke android:width="1.0px" android:color="@color/rounded_container_border" />
                <gradient android:startColor="@color/rounded_container_bg" android:endColor="@color/rounded_container_bg" android:angle="270.0" />
                <corners android:radius="10.0dip" />

            </shape>
        </item>
    </selector>
</inset>

progressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape android:shape="rectangle" >
            <corners android:radius="5dp" />

            <gradient
                android:angle="270"
                android:endColor="@color/light_gray_header_color"
                android:startColor="@color/light_gray_header_color" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape android:shape="rectangle" >
                <corners android:radius="5dp" />

                <gradient
                    android:angle="270"
                    android:endColor="#00996a"
                    android:startColor="#00d190" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape android:shape="rectangle" >
                <corners android:radius="5dp" />

                <gradient
                    android:angle="270"
                    android:endColor="#00996a"
                    android:startColor="#00d190" />
            </shape>
        </clip>
    </item>

</layer-list>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="default_screen_bg">#f9f9f9</color>
    <color name="rounded_container_bg">#222222</color>
    <color name="rounded_container_border">#3b3f44</color>
    <color name="light_gray_header_color">#646663</color>

</resources>

We are good with layout design, lets move a bit into Java coding. Below is my activity code

public class SeekbarActivity extends Activity {

	private SeekBar volumeControl = null;

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

		volumeControl = (SeekBar) findViewById(R.id.volume_bar);

		volumeControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			int progressChanged = 0;

			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
				progressChanged = progress;
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				Toast.makeText(SeekbarActivity.this,"seek bar progress:"+progressChanged, 
						Toast.LENGTH_SHORT).show();
			}
		});
	}

}

Our activity implements SeekBar.OnSeekBarChangeListener; Basically SeekBar.OnSeekBarChangeListener is a public static interface that is used to listen the SeekBar events. The SeekBar.OnSeekBarChangeListener interface allows us to override the below methods.

onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) – Used to notify that the progress level has changed.
onStartTrackingTouch(SeekBar seekBar) – Used to notify that the user has started a touch gesture.
onStopTrackingTouch(SeekBar seekBar) – Used to notify that the user has finished a touch gesture.

Output

Output of the above code is shown below

 Customizing Android Seekbar Example

Download Source Code

Download complete example source code from GitHub

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. Follow him on Twitter or Google plus.

-- Advertisement --
  • radz

    any idea about two thumb range seekbar?

  • barton555

    I’ve done exactly the same. I have one problem in SeekbarActivity class, in 34 line. “seekbar cannot be resolved or is not a field”. I’ve tried to fix this by creating field seekbar in type “menu”, in R.java file but when I tried to run application, R.java file modified to last version, without seekbar field. Any ideas?

    • Alan

      You need to import the seekbar widget on your java file.

      import android.widget.SeekBar;

  • Priyanka

    Nice One Why you not provide source zip code?
    It tooks time to create.

  • Bonhomme

    Oops ignore my previous question, worked it out. Can you help me understand/direct me to a tutorial for the background_view_rounded_single.xml file? I’ve never seen any of those terms before (android:startColor, endcolor, radius, statepressed, etc), and would like to get used to them.

    Thanks for the tutorial, it’s very helpful :)

  • Bonhomme

    Silly question, but does background_view_rounded_single.xml go into the drawables folder, or does it go into the res layout folder as well?

    • http://javatechig.com/ javatechig

      just put it inside drawable folder

    • http://javatechig.com Nilanchala

      Source code link updated.

  • gaon_ka_zamindar

    It will be very helpful from next time if you share the source file.That is what opensource is about I think…

    • http://javatechig.com/ javatechig

      Thanks for your comment. We have missed to upload this piece of work on GitHub. However we have everything inline.

      Thanks.

  • ranjithstar256

    hi thanks for useful example :) where can i download the codes?

  • http://javatechig.com/ javatechig

    All the code files are shown in the example above. Pls have a look into them. thanks

  • Vlad Palamarchuk

    thank you GREAT!

  • @danny117

    Thanks for the seekbar and thanks more for all the xml. I lifted the xml dropped it into a fragment and bam I have a really cool seek that overlays my main screen when I activate the fragment.

  • Cpt.Thomas

    Thx man

  • Karim

    Thanks a lot for this example

  • jorsey

    nice example.