how to use seekbar in android application?

1 comment

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.

Before we start with developing application, download code from here to follow with the below listings.

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


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();

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

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

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

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

//Find the seekbar 4notifyVlmSeekBar = (SeekBar) findViewById(R.id.seekBar4);//Set the max range(Volume in this case) of seekbar//for Notification volumenotifyVlmSeekBar.setMax(audioManager        .getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));//Set the progress with current Notification VolumenotifyVlmSeekBar.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:




1 comment: