Search code examples
androidsettingsseekbarbrightnessscreen-brightness

screen brightness seekbar not working


In my app I added a SeekBar which controls the screen brightness. It works some what fine . I get the device's screen brightness in Oncreate() and set the progress of the SeekBar.

But when I change the seekbar progress , when I set it to the minimum(ie. to the zero position) the device gets stuck and I had to restart my device to get it working again.

What is wring with my code. Please help me

in oncreate() I have the following code,

         try {
            BackLightValue = android.provider.Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS);

            brightnessscrollbar.setProgress(BackLightValue);

            System.out.println("Baclightvalue is "+BackLightValue);


        } catch (SettingNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

seekbarclicklistener

  brightnessscrollbar.setOnSeekBarChangeListener(

          new OnSeekBarChangeListener() {

  @Override

  public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
      // TODO Auto-generated method stub

     BackLightValue = (int) ((float)arg1/100);         
      WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
      layoutParams.screenBrightness = BackLightValue;
      getWindow().setAttributes(layoutParams);
  }
  @Override
  public void onStartTrackingTouch(SeekBar arg0) {
  }
  @Override
  public void onStopTrackingTouch(SeekBar arg0) {

      int SysBackLightValue = (int)(BackLightValue * 255);
        android.provider.Settings.System.putInt(getContentResolver(),
                android.provider.Settings.System.SCREEN_BRIGHTNESS,
                SysBackLightValue);
      }
});

Solution

  • I came to know that Device gets stuck at the value zero

    So I gave a condition on onProgresschanged as folows

    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
      // TODO Auto-generated method stub
    
     BackLightValue = (int) ((float)arg1/100);       
    
      if(arg1>5){  
      WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
      layoutParams.screenBrightness = BackLightValue;
      getWindow().setAttributes(layoutParams);
    
      }
    }
    

    And it works now, like a charm.