Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting full screen brightness in an Android activity

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?

like image 926
Alex Vang Avatar asked May 06 '14 11:05

Alex Vang


People also ask

How to change brightness in Android programmatically?

LayoutParams lp = window. getAttributes(); lp. screenBrightness = (255); window. setAttributes(lp);

How to stop apps from controlling brightness?

Go to Settings → Display → Smart Stay and disable this feature. Check if the brightness level still changes on its own.

How do I limit the brightness on my Android?

Open the Settings app on your phone and head to General > Accessibility > Display Accommodations. At the bottom of this screen, you'll see an option to Reduce White Point. Turn this on and adjust the slider until the screen brightness suits your preferences.


2 Answers

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...

like image 98
Safvan 7 Avatar answered Nov 15 '22 03:11

Safvan 7


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.

like image 24
reVerse Avatar answered Nov 15 '22 05:11

reVerse