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.
- How to Increase/Decrease Screen Brightness in Steps Programmatically in Android?
- How to Maximize/Minimize Screen Brightness Programmatically in Android?
- How to Change the Screen Orientation Programmatically using a Button in Android?
- How to Adjust the Volume of Android Phone Programmatically from the App?
- How to Listen for Volume Button and Back Key Events Programmatically in Android?
- How to Detect Touch Event on Screen Programmatically in Android?
- How to Find the Screen Resolution of a Device Programmatically in Android?
- How to programmatically hide Android soft keyboard
- Current Internet Connection Type in Real-Time Programmatically in Android
- How to Display the List of Sensors Present in an Android Device Programmatically?
- How to Invoke Keyboard Programmatically in Android?
- How to Obtain the Connection Information Programmatically in Android?
- How to Programmatically Enable/Disable Bluetooth in Android?
- How to Programmatically Enable/Disable Wi-Fi in Android?
- How to Control Lottie Animations Programmatically in Android?
- How to Check the Battery Level in Android Programmatically?
- How to Check if the Battery is Charging or Not in Android Programmatically?
- How to Check the Type of Charging (USB/AC) in Android Programmatically?
- Programmatically Check the Network Speed in Android
- How to Display Bluetooth Paired Devices Programmatically in Android?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.