How to Set a Seek Bar in Android

Introduction

 
Hi guys. Today I explain how to work with the seek bar in Android. To demonstrate how it is used to set the Media Player, Ringer, Alarm, and Notification volume for your device. So you can easily understand using the following instructions.
 
Step 1
 
As usual, create a new project file as in the following.
 
newseekbar.jpg
 
Step 2
 
Open the "activity_main.xml" file and update it with the following code:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:paddingBottom="@dimen/activity_vertical_margin"  
  4.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  5.     android:paddingRight="@dimen/activity_horizontal_margin"  
  6.     android:paddingTop="@dimen/activity_vertical_margin"  
  7.     tools:context=".MainActivity"  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="fill_parent"  
  10.     android:orientation="vertical" >  
  11.     <TextView  
  12.         android:id="@+id/textView1"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="Adjust Media player volume"  
  16.         android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px"/>  
  17.     <SeekBar  
  18.         android:id="@+id/seekBar1"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"  
  21.         />  
  22.     <TextView  
  23.         android:id="@+id/textView2"  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="Adjust Ringer volume"  
  27.         android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px" />  
  28.     <SeekBar  
  29.         android:id="@+id/seekBar2"  
  30.         android:layout_width="match_parent"  
  31.         android:layout_height="wrap_content" />     
  32.     <TextView  
  33.         android:id="@+id/textView2"  
  34.         android:layout_width="match_parent"  
  35.         android:layout_height="wrap_content"  
  36.         android:text="Adjust Alarm volume"  
  37.         android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px"/>  
  38.     <SeekBar  
  39.         android:id="@+id/seekBar3"  
  40.         android:layout_width="match_parent"  
  41.         android:layout_height="wrap_content" />  
  42.     <TextView  
  43.         android:id="@+id/textView2"  
  44.         android:layout_width="match_parent"  
  45.         android:layout_height="wrap_content"  
  46.         android:text="Adjust Notification volume"  
  47.         android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px"/>  
  48.     <SeekBar  
  49.         android:id="@+id/seekBar4"  
  50.         android:layout_width="match_parent"  
  51.         android:layout_height="wrap_content" />  
  52. </LinearLayout> 
Step 3
 
Open the "MainActivity.java" file and update it with the following code:
  1. package com.example.androidfourthapp;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. public class MainActivity extends Activity {  
  6.       @Override  
  7.       protected void onCreate(Bundle savedInstanceState) {  
  8.             super.onCreate(savedInstanceState);  
  9.             setContentView(R.layout.activity_main);  
  10.       }  
  11.       @Override  
  12.       public boolean onCreateOptionsMenu(Menu menu) {  
  13.             // Inflate the menu; this adds items to the action bar if it is present.  
  14.             getMenuInflater().inflate(R.menu.main, menu);  
  15.             return true;  
  16.       }  

Step 4
 
Now create a new Java file named "SeekBarExampleAcitivity.java" with the following code:
  1. package com.example.androidfourthapp;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.media.AudioManager;  
  5. import android.os.Bundle;  
  6. import android.widget.SeekBar;  
  7. import android.widget.SeekBar.OnSeekBarChangeListener;  
  8. public class SeekBarExampleAcitvity extends Activity{  
  9.       private SeekBar mediaVlmSeekBar = null;  
  10.       private SeekBar ringerVlmSeekBar = null;  
  11.       private SeekBar alarmVlmSeekBar = null;  
  12.       private SeekBar notifyVlmSeekBar = null;  
  13.       private AudioManager audioManager = null;  
  14.       @Override  
  15.       protected void onCreate(Bundle savedInstanceState) {  
  16.             // TODO Auto-generated method stub  
  17.             super.onCreate(savedInstanceState);  
  18.             setContentView(R.layout.activity_main);  
  19.             this.setVolumeControlStream(AudioManager.STREAM_MUSIC);         
  20.             this.setVolumeControlStream(AudioManager.STREAM_RING);     
  21.             this.setVolumeControlStream(AudioManager.STREAM_ALARM);  
  22.             this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);  
  23.             initControls();  
  24.       }  
  25.       private void initControls() {  
  26.             //Return the handle to a system-level service - 'AUDIO'.  
  27.             audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);  
  28.             //Find the seekbar 1  
  29.             mediaVlmSeekBar = (SeekBar) findViewById(R.id.seekBar1);  
  30.             //Set the max range(Volume in this case) of seekbar  
  31.             //for Media player volume  
  32.             mediaVlmSeekBar.setMax(audioManager  
  33.                     .getStreamMaxVolume(AudioManager.STREAM_MUSIC));  
  34.             //Set the progress with current Media Volume  
  35.             mediaVlmSeekBar.setProgress(audioManager  
  36.                     .getStreamVolume(AudioManager.STREAM_MUSIC));  
  37.             //Find the seekbar 2  
  38.             ringerVlmSeekBar = (SeekBar) findViewById(R.id.seekBar2);  
  39.             //Set the max range(Volume in this case) of seekbar  
  40.             //for Phone ringer volume  
  41.             ringerVlmSeekBar.setMax(audioManager  
  42.                     .getStreamMaxVolume(AudioManager.STREAM_RING));  
  43.             //Set the progress with current Ringer Volume  
  44.             ringerVlmSeekBar.setProgress(audioManager  
  45.                     .getStreamVolume(AudioManager.STREAM_RING));  
  46.             //Find the seekbar 3  
  47.             alarmVlmSeekBar = (SeekBar) findViewById(R.id.seekBar3);  
  48.             //Set the max range(Volume in this case) of seekbar  
  49.             //for Alarm volume  
  50.             alarmVlmSeekBar.setMax(audioManager  
  51.                     .getStreamMaxVolume(AudioManager.STREAM_ALARM));  
  52.             //Set the progress with current Alarm Volume  
  53.             alarmVlmSeekBar.setProgress(audioManager  
  54.                     .getStreamVolume(AudioManager.STREAM_ALARM));  
  55.             //Find the seekbar 4  
  56.             notifyVlmSeekBar = (SeekBar) findViewById(R.id.seekBar4);  
  57.             //Set the max range(Volume in this case) of seekbar  
  58.             //for Notification volume  
  59.             notifyVlmSeekBar.setMax(audioManager  
  60.                     .getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));  
  61.             //Set the progress with current Notification Volume  
  62.             notifyVlmSeekBar.setProgress(audioManager  
  63.                     .getStreamVolume(AudioManager.STREAM_NOTIFICATION));  
  64.             try {  
  65.                 //Listener to receive changes to the SeekBar1's progress level  
  66.                 mediaVlmSeekBar  
  67.                     .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  68.                 public void onStopTrackingTouch(SeekBar arg0) {  
  69.                     }  
  70.                 public void onStartTrackingTouch(SeekBar arg0) {  
  71.                     }  
  72.                 //When progress level of seekbar1 is changed  
  73.                 public void onProgressChanged(SeekBar arg0,  
  74.                     int progress, boolean arg2) {  
  75.                 audioManager.setStreamVolume(  
  76.                     AudioManager.STREAM_MUSIC, progress, 0);  
  77.                 }  
  78.              });  
  79.                ringerVlmSeekBar  
  80.                      .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  81.                public void onStopTrackingTouch(SeekBar arg0) {  
  82.                     }  
  83.                public void onStartTrackingTouch(SeekBar arg0) {  
  84.                     }  
  85.                //When progress level of seekbar2 is changed  
  86.                public void onProgressChanged(SeekBar arg0,  
  87.                     int progress, boolean arg2) {  
  88.                 audioManager.setStreamVolume(  
  89.                     AudioManager.STREAM_RING, progress, 0);  
  90.                  }  
  91.              });  
  92.                alarmVlmSeekBar  
  93.                     .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  94.                public void onStopTrackingTouch(SeekBar arg0) {  
  95.                     }  
  96.                public void onStartTrackingTouch(SeekBar arg0) {  
  97.                     }  
  98.                //When progress level of seekbar3 is changed  
  99.                public void onProgressChanged(SeekBar arg0,  
  100.                     int progress, boolean arg2) {  
  101.                  audioManager.setStreamVolume(  
  102.                         AudioManager.STREAM_ALARM, progress, 0);  
  103.                 }  
  104.              });  
  105.                //Listener to receive changes to the SeekBar4's progress level  
  106.                notifyVlmSeekBar  
  107.                     .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  108.                public void onStopTrackingTouch(SeekBar arg0) {  
  109.                     }  
  110.                
  111.                public void onStartTrackingTouch(SeekBar arg0) {  
  112.                     }  
  113.                //When progress level of seekbar4 is changed  
  114.                public void onProgressChanged(SeekBar arg0,  
  115.                         int progress, boolean arg2) {  
  116.                  audioManager.setStreamVolume(  
  117.                     AudioManager.STREAM_NOTIFICATION, progress,0);  
  118.                 }  
  119.              });  
  120.             } catch (Exception e) {  
  121.                 e.printStackTrace();  
  122.             }  
  123.             }  

Step 5
 
See the output.
 
SeekBarOutput.jpg


Similar Articles