Setting full screen brightness in an Android activity
Asked Answered
M

3

8

I'm using this method to set the screen to full brightness.

@SuppressLint("NewApi") 
private void setFullBright() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
        WindowManager.LayoutParams windowParams = getWindow().getAttributes();
        windowParams.screenBrightness = 1.0f;
        getWindow().setAttributes(windowParams);        
    }
}

If I want the full brightness to be set on the entire life of the Activity's screen, is the onCreate method the best place to call it?

Is there an XML flag that can achieve this? Something like android:keepScreenOn="true" that mirrors the functionality of adding WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON in code?

Mutineer answered 6/5, 2014 at 11:6 Comment(0)
D
5

For everyone who's trying to achieve the same in a DialogFragment. Applying the params to getActivity().getWindow() won't help since the window of the Activity is not the same as the window the Dialog is running in. So you have to use the window of the dialog - see following snippet:

getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
getDialog().getWindow().setAttributes(params);

And to answer the original question: No there is no way to set this via XML.

Diuresis answered 9/4, 2018 at 11:10 Comment(0)
B
20

Put these lines in the oncreate method of all java files which are used to view pages,

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1.0f;
getWindow().setAttributes(params);

This will solve your problem, Happy coding...

Blaeberry answered 7/5, 2014 at 11:11 Comment(2)
You can also use the the constant WindowManager.LayoutParams.BRIGHTESS_OVERRIDE_FULL instead of 1.0F. There is also BRIGHTNESS_OVERRIDE_NONE to reset the brightness to the device setting, and BRIGHTNESS_OVERRIDE_OFF to turn the brightness all the way down.Cohobate
@Bryan: "BRIGHTNESS_OVERRIDE_FULL" not "BRIGHTESS_OVERRIDE_FULL"Geelong
D
5

For everyone who's trying to achieve the same in a DialogFragment. Applying the params to getActivity().getWindow() won't help since the window of the Activity is not the same as the window the Dialog is running in. So you have to use the window of the dialog - see following snippet:

getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
getDialog().getWindow().setAttributes(params);

And to answer the original question: No there is no way to set this via XML.

Diuresis answered 9/4, 2018 at 11:10 Comment(0)
C
3

Kotlin version with constant instead of float: (not for Dialogs)

private fun setScreenBright() {
    with(window){
        addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        attributes = attributes.also { 
            it.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL
        }
    }
}
Casady answered 16/4, 2021 at 10:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.