1

I want to adjust screen brightness by clicking a button, so when the backgroud is white the screen brightness should be maximum, meanwhile if the background is black the screen brightness should be the minimum, but I got an error: NullPointerException... here is my code :

public void lamp2(boolean mode){

        if(mode){

            r.setBackgroundColor(Color.WHITE);
            btn.setText("Turn OFF");
            btn.setTextColor(Color.RED);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = 90 / 100.0f;
            getWindow().setAttributes(lp);
            this.mode = true;
        }

        else if(!mode){

            r.setBackgroundColor(Color.BLACK);
            btn.setText("Turn ON");
            btn.setTextColor(Color.GREEN);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = 100 / 100.0f;
            getWindow().setAttributes(lp);
            this.mode = false;
        }
    }
MD.MD
  • 708
  • 4
  • 14
  • 34

4 Answers4

8

Put these lines to get maximum

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

Put these lines to get minimum

WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0.1f;
getWindow().setAttributes(params);
Safvan 7
  • 395
  • 3
  • 12
0

if you want to manage the brightness of your activity you have to use the following code in your onResume (or onCreate) method:

This code changes the brightness of your screen when called, but doesn't change the system's brigthness parameters (that's the good practice). The brightness will go back to normal when your activity is destroyed (finished). There is no need to ask for permission in your Manifest neither to set back the brightness values when leaving your application. Brief, you this code when managing your screen's brightness.

 override fun onCreate(savedInstanceState: Bundle?) {
        handleScreenBrightness()
        super.onCreate(savedInstanceState)


    }


    private fun handleScreenBrightness() {
        val params = window.attributes
        params.screenBrightness = 1.0f// 0.0 - 1.0
        window.attributes = params
        window.addFlags(WindowManager.LayoutParams.FLAGS_CHANGED)
    }
Sam
  • 6,215
  • 9
  • 71
  • 90
0

Add Bright setting permission in Android Manifest

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

Write Settings is a Protected settings so request user to allow Writing System settings:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.System.canWrite(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivity(intent);
            }
        }

Now you can set Brightness easily

            ContentResolver cResolver = getContentResolver();
            Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, 2);
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
0

Kotlin:

    val lp = window.attributes
    lp.screenBrightness = 0.6F
    window.attributes = lp

All code with Permissions:


    private fun changeSystemBrightness(brightness: Float) {
        if (hasPermissionsToWriteSettings(this)) {
            val lp = window.attributes
            lp.screenBrightness = brightness
            window.attributes = lp
        } else {
            println("not allow permissions")
        }
    }

    private fun hasPermissionsToWriteSettings(context: Activity): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Settings.System.canWrite(context)
        } else {
            ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED
        }
    }
iHTCboy
  • 2,715
  • 21
  • 20