Skip to content
Related Articles

Related Articles

How to Listen for Volume Button and Back Key Events Programmatically in Android?
  • Last Updated : 23 Feb, 2021

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 decreases the volume by some amount. Similarly, the Power key on a Single press locks the device, but on a Long press, Switches on or off the device. This article will create an application that responds to key press and generates a message confirming the same. This idea can further be implemented for creating useful applications:

  1. Gaming Applications: Physical keys could be used for desired actions in Games.
  2. Ambiance: Volume keys could be used to increase or decrease the screen’s brightness rather than doing it the traditional way.
  3. Shortcuts to other Applications: Google Assistant pops up when the home key is pressed for long, similar shortcuts can be made.

This article aims to breach through the codes of this process and use the same keys for creating any desired applications or features. The codes’ scope explained in the latter section of the article is limited to the application that we will create. The created feature or functionality will work only inside the application. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. 

Steps to Listen for Volume Button and Back Key Events Programmatically 

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 MainActivity.kt file

In the MainActivity.kt file, declare an override function onKeyDown and add the following code, as shown in the below. We would generate a Toast in response to the key pressed. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import android.widget.TextView
import android.widget.Toast
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
    }
  
    // 1. onKeyDown is a boolean function, which returns the state of the KeyEvent.
    // 2. This function is an internal function, that functions outside the actual application.
    // 3. When the any Key is pressed, a Toast appears with the following message.
    // 4. This code can be used to check if the device responds to any Key.
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
  
        when (keyCode) {
            KeyEvent.KEYCODE_VOLUME_DOWN -> Toast.makeText(applicationContext, "Volume Down Key Pressed", Toast.LENGTH_SHORT).show()
            KeyEvent.KEYCODE_VOLUME_UP -> Toast.makeText(applicationContext, "Volume Up Key Pressed", Toast.LENGTH_SHORT).show()
            KeyEvent.KEYCODE_BACK -> Toast.makeText(applicationContext, "Back Key Pressed", Toast.LENGTH_SHORT).show()
        }
        return true
    }
}

Output: Run on Emulator

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
My Personal Notes arrow_drop_up
Recommended Articles
Page :