Android - Change Screen Brightness programmatically without permission

How to change brightness programmatically on the Android app screen as well as reflect it on the System level.

It has been commonly practiced by increasing the brightness of the device when scanning or showing QR codes for payment. I’ve come across multiple applications that follow this practice, most of them either wallet or banking apps.

change brightness programmatically-android
Image credit: Pixel 4a Quick Settings (Joe Maring)

Now, there are 2 ways to achieve this thing. With Permission and Without Permission.

Let’s start with the easy one.

Change brightness without Permissions

The easier way is just to adjust the brightness of the screen without affecting the system's brightness.

You can set the screenBrightness attribute of the window, like so

val layout: WindowManager.LayoutParams? = activity?.window?.attributes
layout?.screenBrightness = 0.9f
activity?.window?.attributes = layout

The screenBrightness attribute is a floating-point value ranging from 0 to 1, where 0.0 is 0% brightness, 0.5 is 50% brightness, and 1.0 is 100% brightness.

Let’s take a look at the example below:


Note that this doesn’t affect the brightness for the entire system, only for that particular window. However, in most cases, for most applications, this is probably all you need. In particular, it has the advantage of not requiring elevated permissions, which would be required to change a global system setting.


Change brightness with Permissions

android change brightness programmatically
Image credit: Material Design


The first step would be to add WRITE_SETTINGS permission in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS"
        tools:ignore="ProtectedPermissions"/>

WRITE_SETTINGS is a protected setting that requests the user to allow writing System settings. If you want to know more about permissions, read Android Runtime Permissions

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (!Settings.System.canWrite(this)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }
}


Now when you run your app it will lead you to ask the user to allow the app to modify the system settings from the app. If your app is signed with system certificates then your app will have this permission by default.


The next step is to set the brightness for the app. This time we’ll just pass the value to the system and let the system handle the brightness of the device instead of setting it manually.

Settings.System.putInt(context.getContentResolver(), 
    Settings.System.SCREEN_BRIGHTNESS, brightness);

brightness value should be in the range of 0–255. So, if you have a slider with a range (0-max) then you can normalize the value in the range of (0–255)

private float normalize(float x, float inMin, float inMax, float outMin, float outMax) {
    float outRange = outMax - outMin;
    float inRange  = inMax - inMin;
  	return (x - inMin) *outRange / inRange + outMin;
}

Finally, you can now change the range of the slider value (0–100%) to 0–255 like this

float brightness = normalize(progress, 0, 100, 0.0f, 255.0f);

This would allow your app to pass the brightness to the android system and it will increase the brightness of your device.


Hope you would have liked it!. Please share and subscribe to my blog to support.

Pragnesh Ghoda

A forward-thinking developer offering more than 8 years of experience building, integrating, and supporting android applications for mobile and tablet devices on the Android platform. Talks about #kotlin and #android

Post a Comment

Please let us know about any concerns or query.

Previous Post Next Post

Contact Form