Android Seekbar example

In this post, we will be discussing how to use seek bar in Android application. Before we start with the development, we will learn seekbar and its usage:

What is Seekbar?

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys.

Usage: Seekbar can be used to adjust media player volume, set brightness of the screen, seek a playing music etc.

What are we going to develop?

We will develop a simple application to demonstrate how it is used to set Media player, Ringer, Alarm and Notification volume for your device.

Subscribe and download free ebook
Email:

Quick Links

Project Structure

Layout creation:

  • Create new android project [File >> New >> Android Project] with project name SeekBarExample
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘com.prgguru.android’
  • Click finish

Code Listings

Layout XML:

Open main.xml present under /res/layout folder and replace the XML with the below one.

<?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" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Adjust Media player volume"
        android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px"/>
    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Adjust Ringer volume"
        android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px" />
    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Adjust Alarm volume"
        android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px"/>
    <SeekBar
        android:id="@+id/seekBar3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Adjust Notification volume"
        android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px"/>
    <SeekBar
        android:id="@+id/seekBar4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Application layout will look like:

Change to graphical layout of Main.xml, the layout design should look like below:

Create following objects under SeekBarExampleActivity class to refer seekbars and to get audimanager:

private SeekBar mediaVlmSeekBar = null;
private SeekBar ringerVlmSeekBar = null;
private SeekBar alarmVlmSeekBar = null;
private SeekBar notifyVlmSeekBar = null;
private AudioManager audioManager = null;

Suggests an audio stream whose volume should be changed by the hardware volume controls. Add following snippet inside onCreate method after ‘super.onCreate(savedInstanceState)’

this.setVolumeControlStream(AudioManager.STREAM_MUSIC);		
this.setVolumeControlStream(AudioManager.STREAM_RING);	
this.setVolumeControlStream(AudioManager.STREAM_ALARM);
this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);

A separate method ‘initControls’ to handle the seekbars is going to be created, call it inside onCreate method:

initControls();

Method description of initControls is here, each line is explained in comments:

private void initControls() {
//Return the handle to a system-level service - 'AUDIO'.
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

//Find the seekbar 1
mediaVlmSeekBar = (SeekBar) findViewById(R.id.seekBar1);
//Set the max range(Volume in this case) of seekbar
//for Media player volume
mediaVlmSeekBar.setMax(audioManager
		.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
//Set the progress with current Media Volume
mediaVlmSeekBar.setProgress(audioManager
		.getStreamVolume(AudioManager.STREAM_MUSIC));

//Find the seekbar 2
ringerVlmSeekBar = (SeekBar) findViewById(R.id.seekBar2);
//Set the max range(Volume in this case) of seekbar
//for Phone ringer volume
ringerVlmSeekBar.setMax(audioManager
		.getStreamMaxVolume(AudioManager.STREAM_RING));
//Set the progress with current Ringer Volume
ringerVlmSeekBar.setProgress(audioManager
		.getStreamVolume(AudioManager.STREAM_RING));

//Find the seekbar 3
alarmVlmSeekBar = (SeekBar) findViewById(R.id.seekBar3);
//Set the max range(Volume in this case) of seekbar
//for Alarm volume
alarmVlmSeekBar.setMax(audioManager
		.getStreamMaxVolume(AudioManager.STREAM_ALARM));
//Set the progress with current Alarm Volume
alarmVlmSeekBar.setProgress(audioManager
		.getStreamVolume(AudioManager.STREAM_ALARM));

//Find the seekbar 4
notifyVlmSeekBar = (SeekBar) findViewById(R.id.seekBar4);
//Set the max range(Volume in this case) of seekbar
//for Notification volume
notifyVlmSeekBar.setMax(audioManager
		.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
//Set the progress with current Notification Volume
notifyVlmSeekBar.setProgress(audioManager
		.getStreamVolume(AudioManager.STREAM_NOTIFICATION));

try {
    //Listener to receive changes to the SeekBar1's progress level
    mediaVlmSeekBar
		.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    public void onStopTrackingTouch(SeekBar arg0) {
		}

    public void onStartTrackingTouch(SeekBar arg0) {
		}
    //When progress level of seekbar1 is changed
    public void onProgressChanged(SeekBar arg0,
		int progress, boolean arg2) {
	audioManager.setStreamVolume(
		AudioManager.STREAM_MUSIC, progress, 0);
	}
 });

   //Listener to receive changes to the SeekBar2's progress level
   ringerVlmSeekBar
		 .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   public void onStopTrackingTouch(SeekBar arg0) {
		}

   public void onStartTrackingTouch(SeekBar arg0) {
		}
   //When progress level of seekbar2 is changed
   public void onProgressChanged(SeekBar arg0,
		int progress, boolean arg2) {
	audioManager.setStreamVolume(
		AudioManager.STREAM_RING, progress, 0);
	 }
 });

   //Listener to receive changes to the SeekBar3's progress level
   alarmVlmSeekBar
		.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   public void onStopTrackingTouch(SeekBar arg0) {
		}

   public void onStartTrackingTouch(SeekBar arg0) {
		}
   //When progress level of seekbar3 is changed
   public void onProgressChanged(SeekBar arg0,
		int progress, boolean arg2) {
	 audioManager.setStreamVolume(
	        AudioManager.STREAM_ALARM, progress, 0);
	}
 });

   //Listener to receive changes to the SeekBar4's progress level
   notifyVlmSeekBar
		.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   public void onStopTrackingTouch(SeekBar arg0) {
		}

   public void onStartTrackingTouch(SeekBar arg0) {
		}
   //When progress level of seekbar4 is changed
   public void onProgressChanged(SeekBar arg0,
	        int progress, boolean arg2) {
	 audioManager.setStreamVolume(
		AudioManager.STREAM_NOTIFICATION, progress,0);
	}
 });
} catch (Exception e) {
	e.printStackTrace();
}
}

Demo

Let us test the application:
Run click on the project >> Run as >> Android application
You could see following screen:


Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.

 

Reference:

Android Audio Manager

Android Seekbar

I hope you enjoyed the post!! :)

Suggested posts for further reading



Author: Android Guru

Share This Post On
  • japanjot singh

    very good tutorial, everything is clear and well explained…thnx

  • http://www.atinfo.com dhruvi

    fantastic example

  • Ric

    Its a perfect tutorial.You churned a fan outta me :)

  • ftxbird

    My English is poor, but I still fluent reading your tutorial. Your blog is great, much better than our Chinese site.
    Finally, I will be in your blog the end of the android.

    • http://programmerguru.com/android-tutorial Android Guru

      Welcome to my blog! :)

x
Subscribe and stay in touch!!

Enter your email and stay on top of things:

Your email address will not be published