Blog purpose for android basic example for android app developer. any query please my contact

Sunday 28 January 2024

android studio volume progress bar example

 android studio  volume progress bar example


In this Tutorial Today learn android studio  volume progress bar example


1. activity_main.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <SeekBar

        android:id="@+id/volumeSeekBar"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_centerVertical="true"

        android:layout_marginStart="16dp"

        android:layout_marginEnd="16dp"

        android:thumb="@drawable/ic_volume_thumb"

        android:progressDrawable="@drawable/seekbar_progress"/>


</RelativeLayout>


2.seekbar_progress.xml Using res/drawable folder:

<!-- res/drawable/seekbar_progress.xml -->


<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">

        <shape>

            <solid android:color="#c0c0c0" />

            <corners android:radius="5dp" />

        </shape>

    </item>

    <item android:id="@android:id/progress">

        <clip>

            <shape>

                <solid android:color="#00796B" />

                <corners android:radius="5dp" />

            </shape>

        </clip>

    </item>

</layer-list>



3.ic_volume_thumb.xml using in the res/drawable folder:


<!-- res/drawable/ic_volume_thumb.xml -->


<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <size

        android:width="20dp"

        android:height="20dp" />

    <solid android:color="#00796B" />

    <corners android:radius="10dp" />

</shape>



4.MainActivity.java:

// MainActivity.java


import android.media.AudioManager;

import android.os.Bundle;

import android.widget.SeekBar;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {


    private AudioManager audioManager;

    private SeekBar volumeSeekBar;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

        volumeSeekBar = findViewById(R.id.volumeSeekBar);


        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);


        volumeSeekBar.setMax(maxVolume);

        volumeSeekBar.setProgress(currentVolume);


        volumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);

            }


            @Override

            public void onStartTrackingTouch(SeekBar seekBar) {

                // Do nothing

            }


            @Override

            public void onStopTrackingTouch(SeekBar seekBar) {

                // Do nothing

            }

        });

    }

}




No comments:

Post a Comment