Change the System Brightness Programmatically

You can use following:

// Variable to store brightness value
private int brightness;
// Content resolver used as a handle to the system's settings
private ContentResolver cResolver;
// Window object, that will store a reference to the current window
private Window window;

In your onCreate write:

// Get the content resolver
cResolver = getContentResolver();
 
// Get the current window
window = getWindow();
    
try {
    // To handle the auto
    Settings.System.putInt(
        cResolver,
        Settings.System.SCREEN_BRIGHTNESS_MODE,
        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
    );
    // Get the current system brightness
    brightness = Settings.System.getInt(
        cResolver, Settings.System.SCREEN_BRIGHTNESS
    );
} catch (SettingNotFoundException e) {
    // Throw an error case it couldn't be retrieved
    Log.e("Error", "Cannot access system brightness");
    e.printStackTrace();
}

Write the code to monitor the change in brightness.

then you can set the updated brightness as follows:

// Set the system brightness using the brightness variable value
Settings.System.putInt(
    cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness
);
// Get the current window attributes
LayoutParams layoutpars = window.getAttributes();
// Set the brightness of this window
layoutpars.screenBrightness = brightness / 255f;
// Apply attribute changes to this window
window.setAttributes(layoutpars);

Permission in manifest:

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

For API >= 23, you need to request the permission through Settings Activity, described here: Can't get WRITE_SETTINGS permission


I had the same problem.

Two solutions:

here, brightness =(int) 0 to 100 range as i am using progressbar

1 SOLUTION

float brightness = brightness / (float)255;
WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = brightness;
        getWindow().setAttributes(lp);

2 SOLUTION

I just used dummy activity to call when my progress bar stop seeking.

 Intent intent = new Intent(getBaseContext(), DummyBrightnessActivity.class);
                    Log.d("brightend", String.valueOf(brightness / (float)255));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this is important
                    //in the next line 'brightness' should be a float number between 0.0 and 1.0
                    intent.putExtra("brightness value", brightness / (float)255); 
                    getApplication().startActivity(intent);

Now coming to the DummyBrightnessActivity.class

 public class DummyBrightnessActivity extends Activity{

private static final int DELAYED_MESSAGE = 1;

private Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);            
    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == DELAYED_MESSAGE) {
                DummyBrightnessActivity.this.finish();
            }
            super.handleMessage(msg);
        }
    };
    Intent brightnessIntent = this.getIntent();
    float brightness = brightnessIntent.getFloatExtra("brightness value", 0);
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = brightness;
    getWindow().setAttributes(lp);

    Message message = handler.obtainMessage(DELAYED_MESSAGE);
    //this next line is very important, you need to finish your activity with slight delay
    handler.sendMessageDelayed(message,200); 
}

}

don't forget to register DummyBrightnessActivity to manifest.

hope it helps!!


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

WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1F;
getWindow().setAttributes(layout);     

This code/technique is adapted from a blog entry by Almond Joseph Mendoza on January 5, 2009, entitled "Changing the Screen Brightness Programatically" (archived on the Wayback Machine).

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.

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.