Search code examples
androidandroid-orientation

How to "Change Screen Orientation Settings of whole device"?


I want to change the screen orientation settings of android devices from my app from any mode to "Auto Rotate". This change is not only for my app but for whole device. I have "Root Access" also but if this thing is possible without root access then it will be much better. There are already apps in playstore like "Rotation Control" which does this stuff but I have no idea how it works and I am only getting docs and tutorials to change screen orientation at application level but I want at device level. So at least please guide me a bit or any idea would be helpful.


Solution

  • You can use something like this

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

    dont forget to add permissons

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

    for more details you can see this