2

I want to fully dim my screen brightness programmatically using seekbar I am using this code to do the operation but it does not dim the screen brightness fully.I want to remove the screen brightness completely

code:

public class Night extends AppCompatActivity {

private SeekBar brightbar;
private int brightness;
private ContentResolver contentResolver;
private android.view.Window window;
TextView txtPerc;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_night);

    brightbar=(SeekBar)findViewById(R.id.brightbar);
    txtPerc=(TextView)findViewById(R.id.txtPercentage);
    contentResolver=getContentResolver();
    window=getWindow();
    brightbar.setMax(255);
    brightbar.setKeyProgressIncrement(1);

    try{
        brightness= Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS);
    }catch (Exception e) {
        e.printStackTrace();
    }

    brightbar.setProgress(brightness);

    brightbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            if (progress<=5)
            {
                brightness=5;
            }else {
                brightness=progress;
            }

            float perc=(brightness/(float)255)*100;
            txtPerc.setText((int)perc + "%");
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

            Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS,brightness);
            WindowManager.LayoutParams layoutpars=window.getAttributes();

            layoutpars.screenBrightness=brightness/(float)255;
            window.setAttributes(layoutpars);
        }
    });



}

}

Can anyone suggest me how can I do that.

Adarsh
  • 165
  • 2
  • 16

2 Answers2

1

most "nightmode"/"protect your eyes" kind of apps uses a service with a View covering the whole screen.

they then set the transparency/alpha of this view according to how dark you want. the background color can be different hues or black.

take a look at this question which shows how to create a view from a service Starting a View from a Service?

hope it helps.

Community
  • 1
  • 1
Angel Koh
  • 12,479
  • 7
  • 64
  • 91
0

use bellow code it works for me

 WindowManager.LayoutParams layout = getWindow().getAttributes();
     layout.screenBrightness = 1F;
     getWindow().setAttributes(layout); 
shekhar pande
  • 1,172
  • 1
  • 11
  • 21
  • i just tried this but it lower the screen brightness up to some extent only I want to lower the screen brightness completely – Adarsh Dec 14 '16 at 14:52