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?
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.
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.
📲 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.
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.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With