Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing screen brightness programmatically in android

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?

like image 515
Sri Sri Avatar asked Sep 17 '10 17:09

Sri Sri


People also ask

How do I change brightness on my Android?

Adjust brightness on Android device On an unlocked device, swipe your finger down from the top of the screen twice. Press and hold your finger on the brightness slider (shown below) and drag left or right to adjust the brightness.

How do I override my screen brightness?

That may cause various brightness-related glitches. To disable Adaptive Brightness on a Samsung device, go to Settings → Display. Then toggle off Adaptive Brightness. If you want to disable automatic brightness adjustment on Android phones, go to Settings → Display and brightness and toggle off the Automatic option.

How do I make my Android phone screen brighter than the max?

📲 On Android: Settings > Display > Tap the slider next to Adaptive brightness and switch it to the off position. Then, adjust the brightness bar until you've reached your desired level of brightness.

Is there an app to increase screen brightness?

Velis is a replacement for Android's default auto-brightness feature without all of the other bells and whistles that come with other brightness management apps. The learning curve for Velis is slightly steep, but that's because it gives you maximum control over what you can do.


3 Answers

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

like image 78
Tor-Morten Avatar answered Oct 22 '22 10:10

Tor-Morten


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.

like image 20
AZ_ Avatar answered Oct 22 '22 08:10

AZ_


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.

like image 1
Maurits Rijk Avatar answered Oct 22 '22 10:10

Maurits Rijk