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

Nitish Kumar Singh

Nov 13, 2023 . 3 min read

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

How to Control Volume using Android SeekBar in Java

Hello Developers! In this blog post, we will learn how to change volume using SeekBar in Android using Java. Basically we increase or decrease system volume of phone when progress of SeekBar chnages and show a TextView for volume level.

Nov 15, 2023

How to Remove All Child Elements at Once in JavaScript

Hello, developers! In this blog post, we will learn how to remove all child elements at once from an HTML element using JavaScript. Removing all children at once is a little problematic task on the web because the web does not have any method like removeAll() that removes all children.

Nov 17, 2023

How to Listen to URL Changes in Next.js for a Route or Overall App

Hello Developers, In this blog post, we will see how to listen to URL changes at the route level or application level in a Next.js app. We will create a listener to listen for URL changes in both App Router and Pages Router.

Nov 23, 2023

How to Get Absolute Paths of Internal and External Storages in Android Development

To get paths, we use the ContextCompat.getExternalFilesDirs method. This method returns paths to the files directory (application-specific) on all available external storage devices. The returned paths are not exactly what we want, but we can modify them to suit our needs.

Nov 24, 2023

How to List All Videos in Android App Development using Java

Hello Developers! In this blog post, we will learn how to list or extract all video files with information from Internal (Phone) and External (SD Card) storages in Android app development using Java. For example, if we are developing a Video Player app, we need to show all videos.

Nov 25, 2023

Create an Overlay and Transparent Scrollbar that is Visible on Hover using CSS

Hello, developers! In this blog post, we will learn how to create and style a transparent and overlay scrollbar that is only visible when the scrolling container or content is hovered over. Overlay and transparent scrollbars enhance user experience because their availability does not affect layout size, and they appear over content on hover.

Nov 27, 2023