Skip to content Skip to sidebar Skip to footer

Android 2.2: Adjusting Screen Brightness

public void SetBright(float value) { Window mywindow = getWindow(); WindowManager.LayoutParams lp = mywindow.getAttributes(); lp.screenBrightness = value

Solution 1:

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:

intbrightnessMode= 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.LayoutParamslayoutParams= 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."

Solution 2:

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

Solution 3:

Adjust Brightness using a seekbar :

public class AndroidBrightnessActivity extends Activity {

privateSeekBar brightbar;
private int brightness;
privateContentResolver cResolver;
privateWindowwindow;
TextView txtPerc;
@OverridepublicvoidonCreate(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(newOnSeekBarChangeListener()
    {
        publicvoidonStopTrackingTouch(SeekBar seekBar)
        {
            System.putInt(cResolver,System.SCREEN_BRIGHTNESS,brightness);
            LayoutParams layoutpars = window.getAttributes();
            layoutpars.screenBrightness = brightness / (float)255;
            window.setAttributes(layoutpars);
        }

        publicvoidonStartTrackingTouch(SeekBar seekBar)
        {
            //Nothing handled here
        }

        publicvoidonProgressChanged(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...

Post a Comment for "Android 2.2: Adjusting Screen Brightness"