I am looking to add controls to adjust screen brightness locally in my app menu but can't seem to figure out how to do it. I have seen examples to max-out or dim brightness but I am looking to add controls so that the user can control and set the brightness level. Does anyone have any examples, tutorials, source code, or just a place to point me in the right direction?
            Asked
            
        
        
            Active
            
        
            Viewed 2.8k times
        
    3 Answers
27
            
            
        The internet claims this works, I haven't tried it though:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);
        Jeremy Logan
        
- 47,151
 - 38
 - 123
 - 143
 
- 
                    That works from an Activity...any idea how to do it from a widget? The Power Manager widget for Android 2.0 does it. I want to be able to do it from my widget. – Ryan Alford Dec 11 '09 at 19:53
 - 
                    1Notice, that it may don't work in some cases: http://stackoverflow.com/questions/4611287/changing-screen-brightness-on-a-htc-sense-device – AlexKorovyansky Aug 25 '11 at 09:50
 
10
            
            
        You can change the user's brightness setting like this (make sure you declare permission for WRITE_SETTINGS in the manifest)
android.provider.Settings.System.putInt(getContentResolver(), 
                        android.provider.Settings.System.SCREEN_BRIGHTNESS, brightpref);
The documentation calls that setting from 0 to 255. I am trying to figure out whether it shuts the screen off at 0, as at one point I had a widget installed with a slider and you'd cause the screen to shut off if you set it to 0.
        mylock
        
- 559
 - 4
 - 17
 
1
            
            
        This is answered by a similar question.
The only difference is that you will want to tie the screenBrightness member to the value of a user interface control.
Note that this might not work as expected on devices that have auto-dimming sensors.
        Community
        
- 1
 - 1
 
        Timothy Lee Russell
        
- 3,719
 - 1
 - 35
 - 43