How to change Brightness of Android Layout Programmatically without Permissions

TLDR;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.filters);
    
    WindowManager.LayoutParams layout = getWindow().getAttributes();
    layout.screenBrightness = 1F;
    getWindow().setAttributes(layout);
}

Imagine you have developed a photography app that allows users to edit their photos. The app provides various editing tools and filters to enhance the appearance of the photos. However, some of the editing features, like adjusting brightness, contrast, or shadows, may require the user to have a clear view of the photo.

In this case, you can dynamically increase the brightness of the app screen when the user is actively using the editing features. By doing so, you ensure that the user has a better visual experience and can accurately adjust the photo settings.

There are basically two ways to do it:

  1. By increasing the Android system brightness and
  2. By increasing the brightness of app window

Here’s how you can implement this brightness adjustment in your app using second method:

1. Determine the screen brightness level required for optimal photo editing. You can experiment and find a suitable brightness level that works best for your app’s interface and photo editing features. Let’s assume a brightness level of 0.8 for this example.

2. In your photo editing activity, override the onResume() method:

Override
protected void onResume() {
    super.onResume();
    adjustScreenBrightness(0.8f);
}

@Override
protected void onPause() {
    super.onPause();
    resetScreenBrightness();
}

3. Implement the adjustScreenBrightness() and resetScreenBrightness() methods in your activity:

private void adjustScreenBrightness(float brightnessLevel) {
    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.screenBrightness = brightnessLevel;
    getWindow().setAttributes(layoutParams);
}

private void resetScreenBrightness() {
    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    getWindow().setAttributes(layoutParams);
}

In the adjustScreenBrightness() method, we retrieve the current window attributes and set the screenBrightness property to the desired brightness level (0.8 in this example). Finally, we apply the updated attributes to the window.

In the resetScreenBrightness() method, we reset the brightness level to the default value, using the constant WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE.

By increasing the brightness of the screen during photo editing, your app provides a better visual environment for users to adjust the photo settings accurately. Once the user leaves the photo editing activity (e.g., by navigating to another screen or closing the app), the brightness level is reset to its default value.

Remember to adjust the brightness level according to your specific use case, the value ranges from 0 being lowest brightness to 1 being the highest.

Leave a Reply

Your email address will not be published. Required fields are marked *