3
    public void SetBright(float value) 
{
    Window mywindow = getWindow();

    WindowManager.LayoutParams lp = mywindow.getAttributes();

            lp.screenBrightness = value;

            mywindow.setAttributes(lp);
}

I want to adjust the screen brightness. But nothing happens when i try using this method. Could it be because i use the KEEP_SCREEN_ON flag?

Johan
  • 427
  • 2
  • 7
  • 14

3 Answers3

7

Make sure that "Auto Brightness" is not enabled prior to setting the screen brightness. You can do this manually in Settings>Display or using code if you are using Android 2.2 or above SDK.

Something like:

int brightnessMode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = 0.5F; // set 50% brightness
getWindow().setAttributes(layoutParams);

Ensure the value is between 0.0F and 1.0F. A value of -1.0F uses the default brightness stored in the preferences. Per the documentation "A value of less than 0, the default, means to use the preferred screen brightness. 0 to 1 adjusts the brightness from dark to full bright."

Adam
  • 5,226
  • 4
  • 21
  • 18
  • Just a note, when you paste this into your IDE and it complains about the 'Settings.system ....' make sure that your project is set to use atleast android v 2.3.3 otherwise it won't compile :) – Garbit Feb 10 '12 at 14:08
  • Setting the screen brightness using WindowManager.LayoutParams works even if "Auto Brightness" IS enabled. As stated in the documentation, "screenBrightness can be used to override the user's preferred brightness of the screen". Tested on Android 4.1 and 4.4. – TechAurelian May 06 '14 at 10:09
  • @Adam Thomas can u pls tell me i m aplying values 0.5F but its not working while 0.1,0.2 is working and in value 0.5 its still setting values to 0.2 that means 20% – Erum Jan 06 '15 at 05:45
  • Just a headsup that changing android settings requires android.permission.WRITE_SETTINGS – Dominic Cerisano Jan 29 '15 at 00:50
0

Adjust Brightness using a seekbar :

public class AndroidBrightnessActivity extends Activity {

private SeekBar brightbar;
private int brightness;
private ContentResolver cResolver;
private Window window;
TextView txtPerc;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    brightbar = (SeekBar) findViewById(R.id.brightbar);
    txtPerc = (TextView) findViewById(R.id.txtPercentage);
    cResolver = getContentResolver();
    window = getWindow();
    brightbar.setMax(255);
    brightbar.setKeyProgressIncrement(1);

    try
    {
        brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
    }
    catch (SettingNotFoundException e)
    {
        Log.e("Error", "Cannot access system brightness");
        e.printStackTrace();
    }
    brightbar.setProgress(brightness);
    brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    {
        public void onStopTrackingTouch(SeekBar seekBar)
        {
            System.putInt(cResolver,System.SCREEN_BRIGHTNESS,brightness);
            LayoutParams layoutpars = window.getAttributes();
            layoutpars.screenBrightness = brightness / (float)255;
            window.setAttributes(layoutpars);
        }

        public void onStartTrackingTouch(SeekBar seekBar)
        {
            //Nothing handled here
        }

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {
            if(progress<=20)
            {
              brightness=20;
            }
            else
            {
                brightness = progress;
            }
            float perc = (brightness /(float)255)*100;
            txtPerc.setText((int)perc +" %");
        }
    });   
}

}

Happy Coding...

Safvan 7
  • 395
  • 3
  • 12
0

The value of screenBrightness between 0.0f and 1.0f, maybe your value is too big. I use the WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON flag and also can adjust the screen brightness

Abdul Rahman
  • 2,097
  • 4
  • 28
  • 36
liu
  • 47
  • 1
  • 4