Android and stuff
Android: Lock and unlock screen orientation
Need to temporarily lock the screen orientation on your app? When calling setRequestedOrientation() on your activity, it will no longer allow the orientation to change even when the phone is rotated. So check what the current orientation of the screen is and set it to the same orientation with setRequestedOrientation()
public static void lockCurrentScreenOrientation(Activity a) {
if(a.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
a.setRequestedOrientation(a.getRequestedOrientation());
}
}
To unlock it again, pass ActivityInfo.SCREEN_ORIENTATION_USER:
public static void unlockScreenOrientation(Activity a) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
Locking the orientation works fine but if I unlock (set the orientation to Configuration.ORIENTATION_UNDEFINED), the screen will switch to landscape mode and will still be locked , no matter how I hold the phone at that moment.
You’re right. I hadn’t properly tested this because I was calling finish() on the activity immediately after unlocking it.
I’ve updated the post. If you pass it Configuration.ORIENTATION_SQUARE then the orientation unlocks.
After some research I can tell you why the method doesn’t work properly: You are using 2 different constants: a.getResources().getConfiguration().orientation returns a android.content.res.Configuration constant while setRequestedOrientation requests a ActivityInfo.screenOrientation constant. So the numbers don’t match up. Here’s the new method I made:
public static void lockCurrentScreenOrientation(Activity a) {
if(a.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
{
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else
{
a.setRequestedOrientation(a.getRequestedOrientation());
}
}
public static void unlockScreenOrientation(Activity a) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
I made an extra check to set the orientation to portrait if no orientation is specified (can happen at startup)
I’ve updated the post and the code in my project – thanks a lot, dude!
I might have been a bit fast with my last post. Sorry about that. This method would be perfectly fine if getRequestedOrientation would always return a orientation like landscape, etc. But of course it also returns ActivityInfo.SCREEN_ORIENTATION_USER, which, when set again, won’t change anything.
The result of the improvements is a piece of code that makes use of both your original code and the one I rewrote.
public static void lockCurrentScreenOrientation(Activity a) {
if(a.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED || a.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_BEHIND || a.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_USER)
{
switch (a.getResources().getConfiguration().orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Configuration.ORIENTATION_PORTRAIT:
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Configuration.ORIENTATION_SQUARE:
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
break;
default:
break;
}
//a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else
{
a.setRequestedOrientation(a.getRequestedOrientation());
}
}
public static void unlockScreenOrientation(Activity a) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
There’s still one problem left though: if the screen is reverse-landscape it will be turned back to landscape since the functionality to detect this is not implemented until Android 2.3. See last comment here:
http://stackoverflow.com/questions/5356643/activity-setrequestedorientation-landscape-vs-reverselandscape
Hello, would you explain to me how do you call those two methods from an activity class? I don’t get how to pass an Activity object to this?
Regards.
Pierre
lockCurrentScreenOrientation(this);
oups, sorry for this stupid question, it now works
thx!
Hi again, is there any way to call this from an inner class, I tried to put lockCurrentScreenOrientation(getParent()); but it doesn’t work… Any idea?
lockCurrentScreenOrientation(NameOfYourOutterClass.this);
Got it
Hello.
Is there any way to disable activity restarts when orientation change occurs? The way with setRequestedOrientation does not help with this. I don’t understand why system still restarts applications with locked orientation.
Wait, I cannot fathom it being so starigfhotrward.
8u1WiE ppywljsiarlb
Thank you so much!
Been searching for 30 minutes now and this was the solution. I’m using tabhost and want 3 of 4 tabs to be able to rotate and 1 tab set to portrait. Previously they would rotate until the fixed portrait tab was selected, then they were all stuck in portrait mode. I used your suggestion and added a “setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);” in the onResume of the other 3 tabs and it now works exactly as I had intended.
Thanks again.