Juraj Fulir
Juraj Fulir

Reputation: 31

android Programmatically set screen brightness and keep it

I'm trying to set the screen brightness from my app, but as soon the screen rotates (Auto-Rotate) my brightness is reset to the systems default brightness.

The code I'm using is following:

final WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
lp.screenBrightness = 0.5f;
((Activity) context).getWindow().setAttributes(lp);
((Activity) context).startActivity(new Intent(context, DummyActivity.class));

Upvotes: 1

Views: 1043

Answers (1)

madlymad
madlymad

Reputation: 6540

This is happening because your activity is restarting. You can try adding your window settings code in onCreate of your activity. Make sure that this code is added before setting the view of the activity.

 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     final WindowManager.LayoutParams lp = getWindow().getAttributes();
     lp.screenBrightness = 0.5f;
     getWindow().setAttributes(lp);

     setContentView(R.layout.your_layout_id);
 }

Upvotes: 1

Related Questions