41

I want to change the screen brightness programmatically in android. At the moment I use this code:

WindowManager.LayoutParams lp = getWindow().getAttributes();
float brightness=1.0f;
lp.screenBrightness = brightness;
getWindow().setAttributes(lp);

But this sample code works on cupcake, not on latest versions. I am using the latest version of SDK.. What is the preferred solution for newer Android Versions?

Janusz
  • 187,060
  • 113
  • 301
  • 369
Sri Sri
  • 3,107
  • 7
  • 34
  • 37
  • possible duplicate of [Can't apply system screen brightness programmatically in Android](http://stackoverflow.com/questions/5032588/cant-apply-system-screen-brightness-programmatically-in-android) – bummi Sep 22 '14 at 13:23
  • Increase brightness on show of a dialog http://stackoverflow.com/a/29091233/185022 – AZ_ Mar 17 '15 at 04:23
  • Check this blog for more detailed explanation https://medium.com/p/18be3eecd6b7 – Pragnesh Ghoda シ Jan 23 '23 at 17:17

5 Answers5

81

This is possible to do by using:

WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1F;
getWindow().setAttributes(layout);

See also: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness

Tor-Morten
  • 1,522
  • 2
  • 12
  • 19
  • 4
    but the brightness value didn't get save for the next time device get awake from power off... how to save it consistently not only for one window but it's should be for all..... ???? – kamal_tech_view Dec 16 '11 at 12:26
  • 3
    float prevBrightness = layout.screenBrightness; will get the current brightness. Save this in onSaveInstancedSaved() or in sharedPreference – Patrick May 14 '14 at 14:58
  • 6
    layout.screenBrightness = -1; or any negative value sets the screen brightness back to the preferred one as it was before. – Vikram Gupta Mar 11 '16 at 20:25
  • Also on latest android SDKs(from 5.0 seems) you nead to configure permission request dialog at first launch. Otherwise it won't work on some devices,Samsung s7 ,e.g. – CodeToLife Feb 11 '17 at 06:34
  • This change the brightness for the current activity, is not persistent. To change the brightness persistently, use the Android command: settings put system screen_brightness [value]. The value should be between 0-255 – Oriol Roma Aug 26 '22 at 08:09
  • Check this blog for more detailed explanation https://medium.com/p/18be3eecd6b7 – Pragnesh Ghoda シ Jan 23 '23 at 17:17
4

You have to add params to Window before it is created otherwise it will throw java.lang.IllegalArgumentException: Window type can not be changed after the window is added.

See the example with a android.app.Dialog.Dialog.

final Dialog dialog = new Dialog(this) {
            @Override
            public void onAttachedToWindow() {
                super.onAttachedToWindow();
                WindowManager.LayoutParams layout = getWindow()
                        .getAttributes();
                layout.screenBrightness = 1F;
                getWindow().setAttributes(layout);
            }
        };
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialog.show();

Please note that brightness value is between 0.0F and 1.0F.

AZ_
  • 21,688
  • 25
  • 143
  • 191
1

Too late answer but want to improve..

I tried with Tor-morten's code but it is for particular screen itself, I wanted to change anywhere, I made service for that.

Change brightness according to surrounding light in android

Hope, It will be useful to others.

Community
  • 1
  • 1
Bhavesh Hirpara
  • 22,255
  • 15
  • 63
  • 104
1

How about using the IHardwareService interface for this? An example can be found in this tutorial.

Update: tutorial link still works, but actual code is also available in next answer.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Maurits Rijk
  • 9,789
  • 2
  • 36
  • 53
  • 1
    Wow, an accepted answer * and * a down vote. Maybe the voter would care to explain why he did so? – Maurits Rijk Sep 23 '10 at 20:22
  • 51
    I think he did so because your answer only contains an link to the solution. Stackoverflow is intended to collect examples and solutions directly and linking to sources is only secondary. If your link is dead your answer is totally dead. If the android interfaces change and you have an example an explanation and in link in the answer I could just edit your answer to reflect the api changes. – Janusz Feb 07 '11 at 15:17
  • 2
    Tutorial link is like dead.. no source found @tutorial. – Bhavesh Hirpara Jan 25 '13 at 06:55
  • 6
    DV, This should not be the accepted answer, Example code should be in the answer. Not to mention you are talking to Hidden APIs. – Chris.Jenkins Feb 18 '14 at 14:33
0
final Dialog dialog = new Dialog(act);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog
                .setContentView(R.layout.menubase_brightness_control);
        dialog.setCanceledOnTouchOutside(true);

        SeekBar global_brightness_control = (SeekBar) dialog
                .findViewById(R.id.global_brightness_control);
        global_brightness_control.setMax(255);
        global_brightness_control.setProgress(Settings.System.getInt(
                con.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS));

        global_brightness_control
                .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub

                    }

                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub

                    }

                    public void onProgressChanged(SeekBar seekBar,
                                                  int progress, boolean fromUser) {
                        Settings.System
                                .putInt(con.getContentResolver(),
                                        Settings.System.SCREEN_BRIGHTNESS, progress);
                    }
                });

        dialog.show();
Jayesh Kalkani
  • 191
  • 3
  • 10