How to Adjust the Volume of Android Phone Programmatically from the App?
Last Updated :
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.
Increase or Decrease the Volume without the Volume Bar UI
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.AudioManager
import android.os.Bundle
import android.widget.Button
import 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)
}
}
}
Output: Run on Emulator
Increase or Decrease the Volume with the Volume Bar UI
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.Context
import android.media.AudioManager
import android.os.Bundle
import android.widget.Button
import 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)
}
}
}
Output: Run on Emulator
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.
Mute or Unmute the Device
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.Context
import android.media.AudioManager
import android.os.Bundle
import android.widget.Button
import 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)
}
}
}
Output: Run on Emulator
Similar Reads
How to Obtain the Phone Number of the Android Phone Programmatically?
While creating an android app, many times we need authentication by mobile number. To enhance the user experience, we can auto-detect the mobile number in a mobile system. So let's start an android project! We will create a button, when clicked it will get a mobile number and display it in TextView.
5 min read
How to Change the Whole App Language in Android Programmatically?
Android 7.0(API level 24) provides support for multilingual users, allowing the users to select multiple locales in the setting. A Locale object represents a specific geographical, political, or cultural region. Operations that require these Locale to perform a task are called locale-sensitive and u
5 min read
How to Detect Tablet or Phone in Android Programmatically?
A Mobile is a portable electronic device that allows you to make calls, send messages, and access the internet, among other functions. A tablet is a mobile computing device with a touchscreen display and typically a larger screen size than a smartphone. Both devices are designed to be portable and a
3 min read
How to Programmatically Take a Screenshot on Android?
Ever wanted to take some perfect screenshot of a particular view, or perhaps some UI element ruined your favorite screenshot? Don't worry much, this Geeks for Geeks article will help you achieve it by making an app from scratch. As Below is the code for the title name in this article, we are going t
3 min read
How to Set an Image as Wallpaper Programmatically in Android?
Setting wallpaper in Android programmatically is helpful when the application is fetching wallpapers from the API library and asking the user whether to set the wallpaper for the home screen or not. In this article, it's been discussed how to set a sample image as the home screen wallpaper programma
4 min read
How to Change ActionBar Title Programmatically in Android?
Whenever we develop an Android application and run it, we often observe that the application comes with an ActionBar with the name of the application in it. This happens by default unless explicitly changed. This text is called the title in the application. One can change the title of the applicatio
3 min read
How to Check the Type of Charging (USB/AC) in Android Programmatically?
When altering the frequency of the background updates to scale back the effect of these updates on battery life, checking the present battery level and charging state is a superb place to start. The battery-life impact of performing application updates depends on the battery level and charging state
3 min read
How to Listen for Volume Button and Back Key Events Programmatically in Android?
By production, Android devices are provided with specific physical keys, such as Volume keys, Power key, Back key, Home key, and Activities key. These keys respond to a press. The same keys have particular functionality on the nature of the press. The volume key on a Single press increases or decrea
3 min read
How to Get the Device's IMEI and ESN Programmatically in Android?
Many times while building Android Applications we require a unique identifier to identify the specific mobile users. For identifying that user we use a unique address or identity. For generating that unique identity we can use the android device id. In this article, we will take a look at How to get
4 min read
How to Change App Icon of Android Programmatically in Android?
In this article, we are going to learn how to change the App Icon of an App on the Button Click. This feature can be used when we have an app for different types of users. Then as per the user type, we can change the App Icon Dynamically. Step by Step Implementation Step 1: Create a New Project To
3 min read