25

There are a lot of cool widgets out there that will enable and disable auto rotate on your phone. Disabling it turns it off across all apps on the phone.

Any ideas how they are accomplishing it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Cameron McBride
  • 6,779
  • 11
  • 40
  • 52

4 Answers4

50

This should do the trick for you:

    import android.provider.Settings;
    public static void setAutoOrientationEnabled(Context context, boolean enabled)
    {
          Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
    }

Add permission to the AndroidManifest.xml

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

You can find the documentation here

OneWorld
  • 17,512
  • 21
  • 86
  • 136
Lior
  • 7,845
  • 2
  • 34
  • 34
  • 2
    This seems to work, but it locks you it locks the orientation in landscape for me even if I'm in portrait. I am testing on a honeycomb tablet so I'm not sure if there's a difference.... Is this the expected behavior? – christoff Jun 29 '11 at 20:31
  • Can it be done buy config only, without the permission? I'm trying to find answer for this https://stackoverflow.com/questions/44196296/application-enables-orientation-change-button-is-status-bar . – Lyubimov Roman May 26 '17 at 15:52
10

Always use user specified screen orientation, this will apply whatever orientation user has choose and will not rotate screen if its disabled.

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
Pawan Maheshwari
  • 15,088
  • 1
  • 48
  • 50
3

This is my impementation for this problem. I had to implement a button which had the same function that the lockbutton from the settings menu.

you can use the setRotationScreenFromSettings to solve your issue

public static boolean getRotationScreenFromSettingsIsEnabled(Context context)
{

    int result = 0;
    try
    {
        result = Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION);
    }
    catch (Settings.SettingNotFoundException e)
    {
        e.printStackTrace();
    }

    return result == 1;
}



public static void setRotationScreenFromSettings(Context context, boolean enabled)
{
    try
    {
        if (Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION) == 1)
        {
            Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            Settings.System.putInt(context.getContentResolver(), Settings.System.USER_ROTATION, defaultDisplay.getRotation());
            Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
        }
        else
        {
            Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
        }

        Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);

    }
    catch (Settings.SettingNotFoundException e)
    {
        e.printStackTrace();
    }
}

 private void regirsterLockScreenOrientationChangedListner()
{
    ContentObserver rotationObserver = new ContentObserver(new Handler())
    {
        @Override
        public void onChange(boolean selfChange)
        {
            refreshLockScreenOrientationBtn();
        }
    };

    context.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),
            true, rotationObserver);
}`

Add permission to the AndroidManifest.xml

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

You have to fix it using following code inside onCreate() Function. Working...

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Irshad Khan
  • 5,670
  • 2
  • 44
  • 39