Programmatically Change Brightness In Android Devices

Hitesh Sahu
1 min readOct 27, 2020

How to change brightness level in UI as well as reflect it on System level.

Add Write setting permission in Android Manifest

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

Write Settings is a Protected settings so request user to allow Writing System settings:

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);
}
}

If your app is signed with system certificates than your app will have this permissions by default.

Now you can set Brightness easily

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

brighness value should be in range of 0-255 so if you have a slider with range (0-max) than you can normalize the value in 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 range of 0–100% slider to 0–255 like this

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

Hope it will save someone’s time.

--

--