30

I was searching how to change the brightness of the screen programmatically and I found this it is very good solution and it works nice, but it works only while my app is active. After my application is shutdown then the brightness is returned back the the same value as before I start my app.

I want to be able to change the brightness just like when I press on the brightness button from my power widget. In the power widget that comes from android there are 3 states. One very bright one very dark and one in between. Is it possible to change the brightness just like when someone press on this widget ?

enter image description here

Edit1: I created this and I added permision to my manifest but when the app is started I do not see any changes to the brightness, I tried with 10 with 100 and now with 200 but no changes any suggestions ?

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.provider.Settings.System.putInt(this.getContentResolver(),
        android.provider.Settings.System.SCREEN_BRIGHTNESS, 200);
}
Community
  • 1
  • 1
Lukap
  • 31,523
  • 64
  • 157
  • 244
  • have you tryed these http://stackoverflow.com/questions/3865883/changing-screen-brightness-in-android-emulator not shore if the do change custom brightness proprety – PedroAGSantos Oct 04 '11 at 11:06

5 Answers5

40

This is the complete code I found to be working:

Settings.System.putInt(this.getContentResolver(),
        Settings.System.SCREEN_BRIGHTNESS, 20);

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness =0.2f;// 100 / 100.0f;
getWindow().setAttributes(lp);

startActivity(new Intent(this,RefreshScreen.class));

The code from my question does not work because the screen doesn't get refreshed. So one hack on refreshing the screen is starting dummy activity and than in on create of that dummy activity to call finish() so the changes of the brightness take effect.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
Lukap
  • 31,523
  • 64
  • 157
  • 244
  • Odd that you have to do that. :/ What phone are you using? – Glitch Oct 05 '11 at 10:10
  • 1
    It is not odd, I use htc desire, I tried the code on samsung galaxy also... It is not device issue it is how the android works, it simply doesn't apply the brightness until the win manager get refreshed and he get refreshed by pressing the back, home button or by creating new activity ... – Lukap Oct 05 '11 at 10:58
  • maybe if you use a widget doesn't need to be refreshed but in case you put the code in oncreate , it definitely need additional activity to be started,(at least this was my case) without that dummy activity the app doesn't work property – Lukap Oct 05 '11 at 11:01
  • @Lukap I used intent to call dummy activity in my AppWidget class, but it force close. – Santhosh_pulliman Jan 25 '12 at 12:39
  • This worked for 2.3 on HTC desire (but I never tried this on 4.0) – Lukap Jul 26 '12 at 10:30
  • Will this stick for other activites, or will it only take effect for the current activity? Suppose I want to allow my users to autodim their phone if they choose to. I don't want this code to hinder that. – IgorGanapolsky Nov 18 '12 at 18:54
  • how can i change brightness using button click with values like 100% , 50% , 10 % , 20%, auto and then again 100% , 50% etc @Lukap – Erum Jan 06 '15 at 04:45
13

Use Tor-Morten's solution in that link, but also set the system brightness setting like so:

android.provider.Settings.System.putInt(getContext().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, bright);

Where bright is an integer ranging from 1 to 255.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
Glitch
  • 2,785
  • 1
  • 27
  • 50
  • I get some exception I guess that I do not have some permission,do you know the permission – Lukap Oct 04 '11 at 11:17
  • Does someone know what permissions do I need ? – Lukap Oct 04 '11 at 11:31
  • I add the permission and it doesn't throw exception but this doesn't change the brightness at all, can you tall me what is the problem ? – Lukap Oct 04 '11 at 11:50
  • Try executing the above code before adjusting the Window brightness. I think you may have to wait a small amount of time before you call finish() on the Activity too. So if you're closing it immediately after setting the brightness, perhaps add a small delay. – Glitch Oct 04 '11 at 12:28
  • After I run the activity it stays active I do not close it , but still it doesn't no change the brightness. Is there any place where I can see a complete running code ? – Lukap Oct 04 '11 at 15:02
  • @Lukap its working for values 10,20 but not for 50,100 why ? – Erum Jan 06 '15 at 05:32
  • I think it did adjust but it doesn't refresh the window, so the current brightness don't work. I don't know how to solve it.... – Peter Zhu Apr 24 '15 at 00:32
  • For this answer to work, what I did was to set brightness mode manual first followed by setting the brightness level. Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness); – Kevin Lee Jan 11 '16 at 15:34
10

I have solved this problem today.

As the first you have to put a permission into your AndroidManifest.xml file:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

Where is the exact place to put it in the file?

<manifest>
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <application>
        <activity />
    </application>
</manifest>

This permission says, that you are allowed to change settings that affect other applications too.

Now you can set brightness automatic mode on and off

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);  //this will set the automatic mode on
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);  //this will set the manual mode (set the automatic mode off)

Is the automatic mode turned on or off right now? You can get the information

int mode = -1;
try {
    mode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); //this will return integer (0 or 1)
} catch (Exception e) {}

So, if you want to change brightness manually, you are supposed to set the manual mode first and after that you can change the brightness.

note: SCREEN_BRIGHTNESS_MODE_AUTOMATIC is 1

note: SCREEN_BRIGHTNESS_MODE_MANUAL is 0

You should use this

if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
    //Automatic mode
} else {
    //Manual mode
}

instead of this

if (mode == 1) {
    //Automatic mode
} else {
    //Manual mode
}

Now you can change the brightness manually

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);  //brightness is an integer variable (0-255), but dont use 0

and read brightness

try {
    int brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);  //returns integer value 0-255
} catch (Exception e) {}

Now everything is set properly, but... you can't see the change yet. You need one more thing to see the change! Refresh the screen... so do this:

    try {
        int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);  //this will get the information you have just set...

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = (float) br / 255; //...and put it here
        getWindow().setAttributes(lp);
    } catch (Exception e) {}

Don't forget the permission...

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
PEPAN
  • 141
  • 1
  • 2
  • 1
    +1 for needing to adjust screen_brightness mode to manual. Also, for API 23 and above, setting the permissions in manifest no longer works - see https://developer.android.com/training/permissions/requesting.html. See http://stackoverflow.com/questions/32266425/android-6-0-permission-denial-requires-permission-android-permission-write-sett for how to change the permissions for API 23 and above. – Ying Mar 01 '17 at 16:53
8

Passing the context of the activity while setting up the parameters would also get the job done without any need to start another activity. The following worked for me-

WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.screenBrightness =0.00001f;// i needed to dim the display
this.getWindow().setAttributes(lp);

i had this piece of code inside onSensorChanged() method which dimmed the display whenever it was triggered.

Deepak Negi
  • 893
  • 10
  • 19
  • 4
    Never set it to zero. Set it to 0.00001f. Setting it to zero will cause a few some phones to turn off their screen indefinitely, forcing you to reset the phone by holding down the power button or removing the battery. – Tim Cooke Apr 24 '17 at 00:57
  • 1
    Thanks, modified the answer. – Deepak Negi May 17 '17 at 11:38
4

COMPLEX EXAMPLE:

    try {
        //sets manual mode and brightnes 255
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);  //this will set the manual mode (set the automatic mode off)
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255);  //this will set the brightness to maximum (255)

        //refreshes the screen
        int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = (float) br / 255;
        getWindow().setAttributes(lp);

    } catch (Exception e) {}
PEPAN
  • 141
  • 1
  • 2