How to Maximize/Minimize Screen Brightness 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?
- Applications Streaming Videos: Each frame could be analyzed and compared with the ambient light of the room and accordingly make changes while viewing it to the users.
- Low Battery Situations: Brightness can be set at a low value if the battery level is low.
- If the screen is inactive or unresponded: If the screen is inactive or unresponded, the brightness could be lowered after a specific time-out.
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
Next, go to the activity_main.xml file, which represents the UI of the project. Add two Buttons as shown, one to make the brightness value maximum and the other to make it minimum. Below is the code for the activity_main.xml file.
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...