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:
Place this
android:configChanges="keyboardHidden|orientation|screenSize"
in the<activity>
tag of my activity in the manifest.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); } }
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.