android

How to Adjust the Volume in Android Phone Programmatically in App Using Android Studio in Java Full Project For Beginners


 

package com.example.myvolumecontroller;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
private AudioManager audioManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

seekBar = findViewById(R.id.seekBar);
audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
seekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
}

public void UpButton(View view){
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
seekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
Toast.makeText(this,”Volume Up”, Toast.LENGTH_SHORT).show();
}

public void DownButton(View view){
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
seekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
Toast.makeText(this,”Volume Down”, Toast.LENGTH_SHORT).show();
}
}

 

 

 

<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<Button
android:id=”@+id/button2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”160dp”
android:layout_marginTop=”250dp”
android:layout_marginEnd=”163dp”
android:onClick=”DownButton”
android:text=”@string/down”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintHorizontal_bias=”1.0″
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”160dp”
android:layout_marginTop=”136dp”
android:onClick=”UpButton”
android:text=”@string/up”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />

<SeekBar
android:id=”@+id/seekBar”
android:layout_width=”300dp”
android:layout_height=”0dp”
android:layout_marginStart=”41dp”
android:layout_marginTop=”150dp”
android:layout_marginEnd=”70dp”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintHorizontal_bias=”0.0″
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/button2″ />

</androidx.constraintlayout.widget.ConstraintLayout>

Similar Posts:

    None Found

Leave a Reply

Your email address will not be published. Required fields are marked *