How to Adjust the Volume of Android Phone Programmatically from the App?

15 Oct, 2020
As stated in the title of this article let’s discuss how to adjust the volume of an Android Phone programmatically from the App. Basically, control the volume in the app mean 
Increase or Decrease the Volume without the Volume Bar UI
Increase or Decrease the Volume with the Volume Bar UI
Mute or Unmute the device
So we are going to discuss all these three processes step by step. Note that we are going to implement this project using the Kotlin language. 
To programmatically control the volume in an Android device, follow the following steps:
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
When the setup is ready, go to the activity_main.xml file, which represents the UI of the project. Create two Buttons, one for increasing the volume and the other for decreasing it, one below the other. Below is the code for the activity_main.xml file. 
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">      <Button        android:id="@+id/btnUp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_above="@+id/btnDown"        android:text="+"        />      <Button        android:id="@+id/btnDown"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="-"        />  </RelativeLayout>
Step 3: Working with the MainActivity.kt file
In the MainActivity.kt file, declare the two Buttons and an audio manager (refer to the codes). While setting the on click listeners to the buttons, we would use the audio manager to increase or decrease the volume. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.media.AudioManagerimport android.os.Bundleimport android.widget.Buttonimport androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Declare Buttons        val upBtn = findViewById<Button>(R.id.btnUp)        val downBtn = findViewById<Button>(R.id.btnDown)          // Declare an audio manager        val audioManager = applicationContext.getSystemService(AUDIO_SERVICE) as AudioManager          // At the click of upBtn        upBtn.setOnClickListener {            // ADJUST_RAISE = Raise the volume, FLAG_PLAY_SOUND = make a sound when clicked            audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND)        }        // At the click of downBtn        downBtn.setOnClickListener {            // ADJUST_LOWER = Lowers the volume, FLAG_PLAY_SOUND = make a sound when clicked            audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND)        }    }}
The only change we have to make is to pass the parameter AudioManager.FLAG_SHOW_UI instead of AudioManager.FLAG_PLAY_SOUND in the MainActivity.kt file. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.Contextimport android.media.AudioManagerimport android.os.Bundleimport android.widget.Buttonimport androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Declare Buttons        val upBtn = findViewById<Button>(R.id.btnUp)        val downBtn = findViewById<Button>(R.id.btnDown)          // Declare an audio manager        val audioManager = applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager          // At the click of upBtn        upBtn.setOnClickListener {            // ADJUST_RAISE = Raise the volume, FLAG_SHOW_UI = show changes made to volume bar            audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI)        }          // At the click of downBtn        downBtn.setOnClickListener {            // ADJUST_LOWER = LOWER the volume, FLAG_SHOW_UI = show changes made to volume bar            audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI)        }    }}
It can be seen that the volume increases when “+” is clicked, and similarly, it decreases when “-” is clicked. When the phone becomes completely mute, changes can be seen on the volume bar.
Step 1: Working with the activity_main.xml file
When the setup is ready, go to the activity_main.xml file, which represents the UI of the project. Create two Buttons, one to mute and the other to unmute the device, one below the other. Below is the code for the activity_main.xml file. 
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     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/btnMute"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:text="Mute"        android:layout_above="@+id/btnUnmute"        />      <Button        android:id="@+id/btnUnmute"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="Unmute"        />  </RelativeLayout>
Step 2: Working with the MainActivity.kt file
In the MainActivity.kt file, declare the two Buttons and an audio manager (refer to the codes). While setting the on click listeners to the buttons, we would use the audio manager to mute or unmute the device. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.Contextimport android.media.AudioManagerimport android.os.Bundleimport android.widget.Buttonimport androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Declare Buttons        val muteBtn = findViewById<Button>(R.id.btnMute)        val unmuteBtn = findViewById<Button>(R.id.btnUnmute)          // Declare an audio manager        val audioManager = applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager            muteBtn.setOnClickListener {            // ADJUST_Mute = Mutes the device, FLAG_SHOW_UI = show changes made to volume bar            audioManager.adjustVolume(AudioManager.ADJUST_MUTE, AudioManager.FLAG_SHOW_UI)        }          unmuteBtn.setOnClickListener {            // ADJUST_Unmute = Unmutes the device, FLAG_SHOW_UI = show changes made to volume bar            audioManager.adjustVolume(AudioManager.ADJUST_UNMUTE, AudioManager.FLAG_SHOW_UI)        }    }}
android
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.