Android 2.2: Adjusting screen brightness
Asked Answered
F

3

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?

Fluoroscopy answered 7/1, 2011 at 0:37 Comment(0)
R
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."

Ragwort answered 23/2, 2011 at 11:58 Comment(4)
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 :)Thisbe
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.Izzo
@Ragwort 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%Derr
Just a headsup that changing android settings requires android.permission.WRITE_SETTINGSLully
S
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

Sacrilegious answered 7/1, 2011 at 6:42 Comment(0)
V
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...

Vickers answered 7/5, 2014 at 4:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.