Search code examples
androidandroid-orientationscreen-brightness

Adjust Andriod phone screen brightness using orientation


How can I use the phone orientation to adjust screen brightness? For example, brightness is maximum when portrait and minimum when landscape.


Solution

  • In your activity class use the method below : (Checks Orientation and controls brightness)

     @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
    
            // Checks the orientation of the screen
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                        Settings.System.putInt(context.getContentResolver(),
                   Settings.System.SCREEN_BRIGHTNESS, 0);
            } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                       Settings.System.putInt(context.getContentResolver(),
                   Settings.System.SCREEN_BRIGHTNESS, 100);
            }
        }
    

    Permissions:

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