Reputation: 11
I need to change Android Screen brightness through my app. All the existing answers I've seen are related to window screen brightness but I need device brightness.
Upvotes: 1
Views: 2378
Reputation: 737
According to my experience
1st method.
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 75 / 100.0f;
getWindow().setAttributes(lp);
where the brightness value very according to 1.0f.100f is maximum brightness.
The above mentioned code will increase the brightness of the current window. If we want to increase the brightness of the entire android device this code is not enough, for that we need to use
2nd method.
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, 192);
Where 192 is the brightness value which very from 1 to 255. The main problem of using 2nd method is it will show the brightness in increased form in android device but actually it will fail to increase android device brightness.This is because it need some refreshing.
That is why I find out the solution by using both codes together.
if(arg2==1)
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 75 / 100.0f;
getWindow().setAttributes(lp);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, 192);
}
It worked properly for me
Upvotes: 0
Reputation: 5859
To change device settings for brightness you'll need to do this:
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, brightness);
Where brightness
must be an integer between 0 and 255. Remember, however, that setting screen brightness to 0 will turn the screen off and turning it back on will not be easy. So always check that brightness is >0 (i personally set minimum brightness to 10).
Let me know if you have further questions.
Upvotes: 1