How to Increase/Decrease Screen Brightness using Volume Keys Programmatically in Android?
Screen brightness is one such factor that directly affects the Users as well as the Battery on a device. Android devices are Smart systems and have an inbuilt system for Auto-Brightness. But mostly this feature is unchecked by the users or set off by default. Irrespective of whether this feature is present, set on or off, or absent in any device, a developer must take this opportunity into consideration and develop an optimized application. Anything that is declared inside the application might have an effect on the outside space, i.e., if the screen brightness was changed programmatically from an application, the brightness value might stay unaltered even after exiting the application. So one must try to trace back the originals and set them before a user exits.
Where can we use this feature?
- Non-Media: Non-media (music or video) applications could use the volume buttons to increase or decrease the brightness of a screen in steps.
- Book Reader Applications: Reading books can sometimes be a moody choice, one can increase or decrease brightness directly from the application in steps.
- News Paper Applications: One can increase or decrease brightness directly from the application in steps.
Through this article, we want you to know that volume buttons could be used for increasing and decreasing the brightness of a screen. 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 AndroidManifest.xml file
Controlling the device screen brightness requires a change in root settings, for which declare a uses-permission of WRITE_SETTINGS in the AndroidManifest.xml file.
Below is the code for the AndroidManifest.xml file.
Step 3: Working with the activity_main.xml file
There is nothing to do with the activity_main.xml file. So keep the file as it is.
Step 4: Working with the MainActivity.kt file
Finally, go to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Output: Run on Emulator
Note that before running the application make sure you have granted the required permissions otherwise the application will be crashed.
Please Login to comment...