3

I am trying to set the screen brightness but when I try and get the current window with this.getWindow() I get null. Why is this? I will post all of my code in my setBrightness() method.

System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS,
            brightness);
Window window = getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness / (float) 255;
window.setAttributes(lp);
plast
  • 213
  • 2
  • 11
  • Are you calling `getWindow()` from an `OnClickListener()` or something similar? – Michell Bak Sep 26 '11 at 09:53
  • No just an activity. The activity is not visible and the method is called from another class that receives a notification from the main activity class. I actually wanted it to just be normal class but it seemed that it had to be an activity so it could access the getWindow() method? – plast Sep 26 '11 at 10:01
  • here is the simple way to do it hope it help you http://stackoverflow.com/questions/2937365/increasing-screen-brightness-for-activity/21829712#21829712 – Syed Raza Mehdi May 06 '14 at 11:42

3 Answers3

0

If you are using a seekbar, try these lines,

System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
window.setAttributes(layoutpars);
Safvan 7
  • 395
  • 3
  • 12
0

If you call it from fragment add getActivity() before getWindow() like this

getActivity().getWindow().setAttributes(layoutParams);

And if use seekbar dont let your brogress be 0, cause it can make your screen goes completly dark(on android 2.3)

Penzzz
  • 2,814
  • 17
  • 23
0

Don't use System.putInt()... You already set the screenBrightness in the lp!

The following code works:

Window window = getWindow();
float brightness = 100.0f;
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness/255.0f;
window.setAttributes(lp);
James
  • 24,676
  • 13
  • 84
  • 130
omt66
  • 4,765
  • 1
  • 23
  • 21
  • Thanks for the input! Still getting null from getWindow() though. Maybe I am retrieving the wrong window somehow? – plast Sep 26 '11 at 09:39
  • verify your imports i think you should import android.view.Window – Houcine Sep 26 '11 at 10:10
  • These are my view imports: import android.view.Window; import android.view.WindowManager; – plast Sep 26 '11 at 10:26
  • when you call the method getwindow() ; is it on your onCreate() method ? – Houcine Sep 26 '11 at 10:45
  • No it is in a different method. – plast Sep 26 '11 at 13:01
  • is that method is on your Activity Class or another class? if yes , so you should pass the Context to your second class ,please add de code of your method and the name of the class that contains this method , :) – Houcine Sep 26 '11 at 14:47
  • 1
    it seems that you calling the getWindow() method from another class which is not an Activity , so you should pass the context to the second Class, the constructor of your class will be : public SecondClass(Context _context){ this.context = _context; } , and then you should call the getWindow() method by using the variable context like this : this.context.getWindow(); hope it helps – Houcine Sep 26 '11 at 17:59