0

Using code below, I can enable or disable auto-rotation for whole device

Settings.System.putInt(
    getActivity().getContentResolver(),
    Settings.System.ACCELEROMETER_ROTATION, 
    orientationEnabled //0 means off, 1 means on
);

How can I do same but restricted to my app? I dont want to change global settings forcing user to restore them, after he used my app.

EDIT: I want to change that setting on runtime, not to lock it permanently.
E.g at some point to enable auto rotation, so use can change orientation, if he want, but not to force him (thus this solution does not fit).

Community
  • 1
  • 1
Yarh
  • 4,459
  • 5
  • 45
  • 95

2 Answers2

2

To temporally disable orientation changes in an Activity call this:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

and then to re-enable orientation changes call:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
1

U can enable/disable screen rotation in AndroidManifest inside activity tag

Like this

 <activity
           ---
            android:screenOrientation="locked"
           ----  />

All screenOrientation options

android:screenOrientation=["unspecified" | "behind" |
                                     "landscape" | "portrait" |
                                     "reverseLandscape" | "reversePortrait" |
                                     "sensorLandscape" | "sensorPortrait" |
                                     "userLandscape" | "userPortrait" |
                                     "sensor" | "fullSensor" | "nosensor" |
                                     "user" | "fullUser" | "locked"]

Hope this will help you.

ArbenMaloku
  • 548
  • 4
  • 15