Azurespot
Azurespot

Reputation: 3152

ActivityInfo.SCREEN_ORIENTATION_LOCKED is not locking orientation - Android

This is a feature in API 18, and I can't get it to actually lock. I have been reading all night on how to manually handle your own configuration change, and tried many things, but the current code I have is not effective.

Goal: I need to have an activity, where, after the user takes a video (which brings them back to the app), the landing page should be either landscape or portrait, whichever way the user is holding the phone. But if they decide to go to landscape, I want to lock that position, so they cannot return to portrait (it causes video viewing errors if they go back). So they have a choice in the beginning, but then must stay in landscape if they choose that one. But how do I do this?

Here's what I have tried so far:

  1. Place this android:configChanges="keyboardHidden|orientation|screenSize" in the <activity> tag of my activity in the manifest.

  2. Override the onConfigurationChanged() method with this:

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
    
            if (getResources().getConfiguration().orientation ==
                    Configuration.ORIENTATION_LANDSCAPE) {
                setContentView(R.layout.activity_make_photo_video_land);
            } else {
                setContentView(R.layout.activity_make_photo_video);
            }
    
            if (this.getResources().getConfiguration().orientation ==
                    ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE && Build.VERSION.SDK_INT
                    >= Build.VERSION_CODES.JELLY_BEAN_MR2){
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
            }
        }
    
  3. I have 2 layouts of xml, one for my portraits in layout folder, and one for landscapes in layout-land folder.

The result I am getting is that I can get the layouts to show on orientation change, but I cannot get it to lock. I am using a device with KitKat, so it should lock on there, but it doesn't.

Every post on here I've read about locking orientation (or workarounds for it) seems to be 3 or 4 years old and basically suggest what I am already trying. Any new information that anyone has out there?

Thanks.

Upvotes: 1

Views: 5643

Answers (4)

Ram Iyer
Ram Iyer

Reputation: 1679

This could be a late answer. People still having trouble can read on.

You don't need to override onConfigurationChanged().

Just setting the flags for the Activity in the Manifest should do, as below:

<activity android:name=".MainActivity"
            android:screenOrientation="landscape"
            android:configChanges="orientation|keyboardHidden"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">

...
...

</activity>

If you still have trouble, you can set multiple orientation flags such as this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int o = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    o |= ActivityInfo.SCREEN_ORIENTATION_LOCKED;
    setRequestedOrientation(o);
    setContentView(R.layout.activity_main);
    ...
    ...
}

The above Java code locks the activity down to Landscape mode. There was never a need to set the orientation flag in Java. The Manifest route should work.

Upvotes: 0

Green goblin
Green goblin

Reputation: 9996

Did you try this?:

public static void lockOrientation(Activity activity) {
        Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int rotation = display.getRotation();
        int currentOrientation = activity.getResources().getConfiguration().orientation;
        int orientation = 0;
        switch(currentOrientation)
        {
        case Configuration.ORIENTATION_LANDSCAPE:
            if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            else
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
            break;
        case Configuration.ORIENTATION_PORTRAIT:
            if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            else
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        }
        activity.setRequestedOrientation(orientation);
    }

    public static void unlockOrientation(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }

Upvotes: 2

gkee
gkee

Reputation: 771

You can do it this way in your Java code:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

See http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int) for more info.

BUT - this is not really the best idea. All you are doing is hiding the error, which can occur in other ways (e.g. if the user locks the screen or receives a phone call while your video is playing). Orientation locking is hardly ever the answer. You should read this: http://developer.android.com/guide/topics/resources/runtime-changes.html

Upvotes: 0

Pranav P
Pranav P

Reputation: 1815

Use the android:scrreenOrientation="portrait" to fix(lock) your screen orientation and put it in your android mainfest file under the perticular activity. for portrait use "portrait" and for landscape use "lanscape". In my case it help me i think it will be help you try this.

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

Upvotes: 0

Related Questions