How to Listen for Volume Button and Back Key Events Programmatically in Android? - GeeksforGeeks

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:
Gaming Applications: Physical keys could be used for desired actions in Games.Ambiance: Volume keys could be used to increase or decrease the screen’s brightness rather than doing it the traditional way.Shortcuts to other Applications: Google Assistant pops up when the home key is pressed for long, similar shortcuts can be made.
Gaming Applications: Physical keys could be used for desired actions in Games.
Ambiance: Volume keys could be used to increase or decrease the screen’s brightness rather than doing it the traditional way.
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. 
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.AppCompatActivityimport android.os.Bundleimport android.view.KeyEventimport android.widget.TextViewimport 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    }}
Android-Misc
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Broadcast Receiver in Android With Example
How to Create and Add Data to SQLite Database in Android?
Services in Android with Example
Content Providers in Android with Example
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Content Providers in Android with Example
Services in Android with Example
Android UI Layouts
Android RecyclerView in Kotlin