Set Activity Screen Brightness without Permission

I wanted to brighten the screen, but all articles said you needed Android permissions. Codswallop! Here’s how!

Set Activity Screen Brightness without Permission

The reason I needed screen brightness without Android permissions was so that QR codes could have the most chance of successful scanning (whilst avoiding drop off when people reject the permission). Turning the brightness to full will give you the max contrast for your QR code legibility.

Maybe my Google foo wasn’t up to scratch but all articles I found said you needed Android permissions. I could’t find a recent article stating a way to avoid this.

I kept bumping in to android.permission.WRITE_SETTINGS answers, which makes sense if you want to change the brightness for your system as well, but when you want to just adjust the brightness temporarily & programatically for your own Activity this is best to be avoided.

Here is how:

window.attributes = window.attributes.also { 
    it.screenBrightness = 1f 
}

Takes a float value between 0 – 1. With 1 being max brightness.

window comes from the Activity context, so you can do this from onCreate or anywhere you have an activity scoped context.

The brightness will reset itself when your activity is no longer visible.

Here are the official docs. And a constant if you want it for BRIGHTNESS_OVERRIDE_FULL.

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.

https://developer.android.com/reference/android/view/WindowManager.LayoutParams#screenBrightness

That’s it! Easy 🙂

Shout out to Ed Holloway-George on this one, when you know you know!

Leave a Reply

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