Tutorial #68: Implement SeekBar in Android

In one of my previous posts, I had talked about implementing a RatingBar in Android. Apart from that, another commonly used Widget in Android is the SeekBar. Basically, a SeekBar acts as an extension of the ProgressBar. Users can change its value using either touch event, draggable thumb or left right arrow keys.

By default the SeekBar has a maximum value of 100. This value can be changed using android:maxattribute in the layout xml file. In addition to this, developers can use android:progressattribute to set the initial progress value. If we set the progress attribute to 5, then during initial loading the SeekBar thumb will start at 5. Now, let’s put this into practice and learn how to implement a SeekBar in Android.

Pre-requisites: Eclipse Helios, Android SDK

Step 1: Create Android project

Open Eclipse IDE and create a new Android Application project called AndroidSeekBarDemo with package name com.example

Step 2: Add SeekBar to layout

We need to first add the SeekBar to our layout file. Create a new XML file named activity_seek under res/layout folder and add the following code!

res/layout/activity_seek.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <SeekBar
        android:id="@+id/mySeekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="500"
        android:progress="50"/>

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text=""/>

</LinearLayout>

Step 3: Create Activity class

Now, create a new Activity class named MySeekBarActivity and add the following code!

MySeekBarActivity.java

package com.example;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class MySeekBarActivity extends Activity implements OnSeekBarChangeListener {

	private SeekBar mSeekBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_seek);
		mSeekBar = (SeekBar) findViewById(R.id.mySeekBar);
		mSeekBar.setOnSeekBarChangeListener(this);
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		Toast.makeText(MySeekBarActivity.this, "Value : " + progress, Toast.LENGTH_LONG).show();
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		Log.i("App","Start Tracking Seekbar");
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		mSeekBar.setSecondaryProgress(seekBar.getProgress());
		Log.i("App","Stop Tracking Seekbar");
	}
}

Finally, run the application on an Android device or emulator and you should see the following output!

android_seekbar_1

android_seekbar_2

Karan Balkar About Karan Balkar
self proclaimed extraterrestrial fandroid, computer engineer, amateur gamer and die hard punk rock fan!

3 thoughts on “Tutorial #68: Implement SeekBar in Android

Leave a Reply

Your email address will not be published. Required fields are marked *