How to add volume slider to android action bar?

2019-06-04 21:01发布

问题:

I want to add volume to action bar. When user clicks on that action it should place slider inside action bar for selecting right volume How would i do that

回答1:

The easiest way to handle this is to use ActionBar.setCustomView.

Here's an example that controls the media volume:

First, you'll need to create the custom layout that contains your SeekBar.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SeekBar
        android:id="@android:id/progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

</FrameLayout>

Now implement SeekBar.OnSeekBarChangeListener in your Activity or Fragmentand declare a couple of variables:

/** Used to actually adjust the volume */
private AudioManager mAudioManager;
/** Used to control the volume for a given stream type */
private SeekBar mVolumeControls;
/** True is the volume controls are showing, false otherwise */
private boolean mShowingControls;

In onCreate or wherever you choose to, inflate and apply the custom View to the ActionBar.

    // Control the media volume
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    // Initialize the AudioManager
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    // Inflate the custom ActionBar View
    final View view = getLayoutInflater().inflate(R.layout.view_action_bar_slider, null);
    mVolumeControls = (SeekBar) view.findViewById(android.R.id.progress);
    // Set the max range of the SeekBar to the max volume stream type
    mVolumeControls.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
    // Bind the OnSeekBarChangeListener
    mVolumeControls.setOnSeekBarChangeListener(this);

    // Apply the custom View to the ActionBar
    getActionBar().setCustomView(view, new ActionBar.LayoutParams(MATCH_PARENT, MATCH_PARENT));

To toggle the controls when your MenuItem is pressed, call ActionBar.setDisplayShowCustomEnabled and don't forget to update the SeekBar progress to the current volume level you're controlling.

        // Toggle the custom View's visibility
        mShowingControls = !mShowingControls;
        getActionBar().setDisplayShowCustomEnabled(mShowingControls);
        // Set the progress to the current volume level of the stream
        mVolumeControls.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));

To control the volume stream, in OnSeekBarChangeListener.onProgressChanged call AudioManager.setStreamVolume

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    // Adjust the volume for the given stream type
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}

And finally in OnSeekBarChangeListener.onStopTrackingTouch, remove the custom controls to display the ActionBar normally again.

    // Remove the SeekBar from the ActionBar
    mShowingControls = false;
    getActionBar().setDisplayShowCustomEnabled(false);

Here's a picture of before and after you press the MenuItem.



回答2:

Here is an answer of a similar question. He wanted to add Full audio Control buttons. You will find how to add the control Seekbar into your actionBar and how to handle it.

I hope it helps