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
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(R.id.btnUp)
val downBtn = findViewById(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)
}
}
}
XML
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(R.id.btnMute)
val unmuteBtn = findViewById(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)
}
}
}
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(R.id.btnUp)
val downBtn = findViewById(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)
}
}
}
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(R.id.btnUp)
val downBtn = findViewById(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)
}
}
}
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(R.id.btnMute)
val unmuteBtn = findViewById(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)
}
}
}