How to get current screen's brightness in Android code dynamiclly?
-
2possible duplicate of [Adding screen brightness controls to android application](http://stackoverflow.com/questions/1791340/adding-screen-brightness-controls-to-android-application) – Vladimir Ivanov Dec 28 '10 at 09:31
-
have you got a way to get it? – DàChún Sep 16 '16 at 10:04
4 Answers
Hi to get the current brightness level of the android system you can use this code:
try {
float curBrightnessValue=android.provider.Settings.System.getInt(
getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
-
5Nope, it reads current settings of brightness, not the current brightness. – greenoldman Aug 02 '12 at 14:56
-
2@greenoldman That is true. Current brightness can be retrieved by `getWindow().getAttributes().screenBrightness`. If however this current brightness is < 0 this means that application uses the current settings for brightness (which can be loaded by the means provided in current answer) – Alex Semeniuk Dec 16 '13 at 10:18
-
`getInt()` Method return Int value.But Brightness store as a float.Better to use `getFloat()` – Elshan May 20 '14 at 04:33
-
@Jimmer Nope, as it returns `The screen backlight brightness between 0 and 255.` – Muhammed Refaat Sep 30 '14 at 10:25
-
@MuhammedRefaat, Android can change brightness automatically (hint: dim). – greenoldman Sep 30 '14 at 12:11
-
@greenoldman So, we have here to set the settings of `SCREEN_BRIGHTNESS_MODE_MANUAL` to avoid that, right ? – Muhammed Refaat Sep 30 '14 at 12:27
This was asked a while ago but just to expand on fiction's answer:
settings.screenBrightness will return -1 if it has not been previously overwritten in code. This is correct behaviour as setting screenBrightness to -1 will set the brightness to the current system brightness level.
This system brightness can be changed by the user at any time, so there is probably not much use in storing the actual value, if you are just trying to return the brightness to its original value, as the actual value might be "out of date".

- 561
- 5
- 5
-
This answer should get more votes since it's probably what most people are looking for. – Marco7757 Dec 14 '18 at 13:41
You can read content of this system file
/sys/class/leds/lcd-backlight/brightness
This value is current screen brightness within range 0-255

- 71
- 4
Acording to this - http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness
you can change brightness by
WindowManager.LayoutParams settings = getWindow().getAttributes();
settings.screenBrightness = newValue;
getWindow().setAttributes(settings);
Also refer Hamiora answer for further explanation.

- 11,167
- 5
- 49
- 74
-
3hi, float savedBrightness = settings.screenBrightness; cannot get the current brightness but whether is overrided by app, and i try it but always get -1.0f from it, because I do not override it! – yuankai Dec 29 '10 at 02:17
-
@yuankai You should perform additional check. If your `savedBrightness` < 0 that means that application uses the default brightness. The default brightness level can be retrieved by MidhunVP's answer. (just keep in mind that `settings.screenBrightness` ranges from 0f to 1f while system setting has values from 0 to 255) – Alex Semeniuk Dec 16 '13 at 10:21