简体   繁体   中英

How to lock android screen orientation

I'm making an Android video player.It has a function like that user can watch video in any orientation.I just use code as follow:

Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);

It works, but when I add a function that user can lock the orientation, I just did it:

Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);

So I met some trouble. when I'm in landscape orientation and try to lock the orientation, the screen just turns to portrait. Can anyone solve it or tell me another way to do with it?

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

OR

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);   

More info here: Developing Orientation-Aware Android Applications

Use following code Based up on your condition change if else statement

int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) 
     {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
     }
 else {
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
       }

or

you can set anyone landscape or portrait in your activity.its never change during the screen rotation

<activity android:name="MyActivity"
 android:screenOrientation="landscape"
 android:configChanges="keyboardHidden|orientation|screenSize">
  ...
</activity>

In your AndroidManifest.xml, for each activity put

android:screenOrientation="landscape"

It forces the activity to landscape.

just you have ot add following property in you manifest.xml file.

android:screenOrientation="portrait"

like this way,

<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
        </activity>

That.sit

You can request ScreenOrinentation public void setRequestedOrientation (int requestedOrientation) . You can use it like this

// For landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
 //OR for portrait 
 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
//OR reverse landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
 //OR for reverse portrait
 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

requestedOrientation An orientation constant as used in ActivityInfo.screenOrientation.

Added in API level 1 The preferred screen orientation this activity would like to run in. From the screenOrientation attribute, one of SCREEN_ORIENTATION_UNSPECIFIED, SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, SCREEN_ORIENTATION_USER, SCREEN_ORIENTATION_BEHIND, SCREEN_ORIENTATION_SENSOR, SCREEN_ORIENTATION_NOSENSOR, SCREEN_ORIENTATION_SENSOR_LANDSCAPE, SCREEN_ORIENTATION_SENSOR_PORTRAIT, SCREEN_ORIENTATION_REVERSE_LANDSCAPE, SCREEN_ORIENTATION_REVERSE_PORTRAIT, SCREEN_ORIENTATION_FULL_SENSOR, SCREEN_ORIENTATION_USER_LANDSCAPE, SCREEN_ORIENTATION_USER_PORTRAIT, SCREEN_ORIENTATION_FULL_USER, SCREEN_ORIENTATION_LOCKED,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM