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.
Step 2
Open the "activity_main.xml" file and update it with the following code:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity"
- 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>
Step 3
Open the "MainActivity.java" file and update it with the following code:
- package com.example.androidfourthapp;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Step 4
Now create a new Java file named "SeekBarExampleAcitivity.java" with the following code:
- package com.example.androidfourthapp;
- import android.app.Activity;
- import android.content.Context;
- import android.media.AudioManager;
- import android.os.Bundle;
- import android.widget.SeekBar;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- public class SeekBarExampleAcitvity extends Activity{
- private SeekBar mediaVlmSeekBar = null;
- private SeekBar ringerVlmSeekBar = null;
- private SeekBar alarmVlmSeekBar = null;
- private SeekBar notifyVlmSeekBar = null;
- private AudioManager audioManager = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
- this.setVolumeControlStream(AudioManager.STREAM_RING);
- this.setVolumeControlStream(AudioManager.STREAM_ALARM);
- this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);
- initControls();
- }
- private void initControls() {
-
- audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
-
- mediaVlmSeekBar = (SeekBar) findViewById(R.id.seekBar1);
-
-
- mediaVlmSeekBar.setMax(audioManager
- .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
-
- mediaVlmSeekBar.setProgress(audioManager
- .getStreamVolume(AudioManager.STREAM_MUSIC));
-
- ringerVlmSeekBar = (SeekBar) findViewById(R.id.seekBar2);
-
-
- ringerVlmSeekBar.setMax(audioManager
- .getStreamMaxVolume(AudioManager.STREAM_RING));
-
- ringerVlmSeekBar.setProgress(audioManager
- .getStreamVolume(AudioManager.STREAM_RING));
-
- alarmVlmSeekBar = (SeekBar) findViewById(R.id.seekBar3);
-
-
- alarmVlmSeekBar.setMax(audioManager
- .getStreamMaxVolume(AudioManager.STREAM_ALARM));
-
- alarmVlmSeekBar.setProgress(audioManager
- .getStreamVolume(AudioManager.STREAM_ALARM));
-
- notifyVlmSeekBar = (SeekBar) findViewById(R.id.seekBar4);
-
-
- notifyVlmSeekBar.setMax(audioManager
- .getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
-
- notifyVlmSeekBar.setProgress(audioManager
- .getStreamVolume(AudioManager.STREAM_NOTIFICATION));
- try {
-
- mediaVlmSeekBar
- .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
- public void onStopTrackingTouch(SeekBar arg0) {
- }
- public void onStartTrackingTouch(SeekBar arg0) {
- }
-
- public void onProgressChanged(SeekBar arg0,
- int progress, boolean arg2) {
- audioManager.setStreamVolume(
- AudioManager.STREAM_MUSIC, progress, 0);
- }
- });
- ringerVlmSeekBar
- .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
- public void onStopTrackingTouch(SeekBar arg0) {
- }
- public void onStartTrackingTouch(SeekBar arg0) {
- }
-
- public void onProgressChanged(SeekBar arg0,
- int progress, boolean arg2) {
- audioManager.setStreamVolume(
- AudioManager.STREAM_RING, progress, 0);
- }
- });
- alarmVlmSeekBar
- .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
- public void onStopTrackingTouch(SeekBar arg0) {
- }
- public void onStartTrackingTouch(SeekBar arg0) {
- }
-
- public void onProgressChanged(SeekBar arg0,
- int progress, boolean arg2) {
- audioManager.setStreamVolume(
- AudioManager.STREAM_ALARM, progress, 0);
- }
- });
-
- notifyVlmSeekBar
- .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
- public void onStopTrackingTouch(SeekBar arg0) {
- }
-
- public void onStartTrackingTouch(SeekBar arg0) {
- }
-
- public void onProgressChanged(SeekBar arg0,
- int progress, boolean arg2) {
- audioManager.setStreamVolume(
- AudioManager.STREAM_NOTIFICATION, progress,0);
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Step 5
See the output.