How to Change Brightness of Only One Activity in Android Using Java

Nitish Kumar Singh

Nov 13, 2023

Hello Developers! In this blog post, we will learn how to change the brightness of only one activity, not for the overall application or the entire phone brightness. For example, if the user drags on the screen, changes the progress of the seek bar, or clicks on buttons, then the brightness of the activity should change. This is a feature commonly seen in Video-Player Apps.

Photo by Sean Oulashin on Unsplash

Note: In this post, I will demonstrate only which fields or classes we need to use for this purpose, how to use them, and provide an overview of achieving this by dragging on the screen and changing the progress of the seek bar.

To implement this feature, we need to modify the screenBrightness field of WindowManager.LayoutParams between zero (lowest brightness level) to one (highest brightness level) in the following steps:

  1. Get an object (let's call it window) of Window by calling the getWindow() method of Activity.
  2. Obtain an object of WindowManager.LayoutParams (let's call it layoutParams) by calling the getAttributes() method of the Window class on the window object.
  3. Change the screenBrightness field of the layoutParams object according to user interaction, ranging from zero (dark) to one (full bright). Setting any value less than zero changes the activity brightness to the user's preferred brightness of the screen.
  4. Finally, set this modified layoutParams object on the window object by calling the setAttributes() method of Window.

Here's the code for this:

Window window = getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.screenBrightness = value; // where value is a float from 0 to 1;
window.setAttributes(layoutParams);

The WindowManager.LayoutParams class has many more fields to experiment with. You can explore more about this class here, and below are the docs about the screenBrightness field from the Android Developer Documentation.

This can be used to override the user's preferred brightness of the screen. A value of less than 0, the default, means to use the preferred screen brightness. 0 to 1 adjusts the brightness from dark to full bright.

Overview of Implementing This with Different Methods:

  • Using Seekbar: Implementing this with a SeekBar is easy. Just add SeekBar.OnSeekBarChangeListener to the SeekBar and change the screenBrightness field in the onProgressChanged method of this listener.
  • Using Drag on ScreenImplementing this with dragging on the screen is a bit more challenging. Set View.OnTouchListener on a view that covers almost the entire screen. In the onTouch method of this listener, get touch points of the pointer (finger) on every touch event, store it for the next event, and process it to make it fall in the range of 0 to 1 for setting as the value of the screenBrightness field. Note that it can be challenging because touch events are fired rapidly, and the difference between two consecutive touch points is almost greater than one.

I hope you enjoyed this blog post and learned how to change the brightness of an activity in Android app development. Get an idea of doing it by using a seekbar or dragging on the screen.

Published on Nov 13, 2023
Comments (undefined)

Read More