How can I prevent my app form rotating. I want it so if the user is in portrait mode, the app will always be in portrait mode, and if they start the app in landscape the app will always be in lanscape (until the app is closed and started again)
Asked
Active
Viewed 335 times
1 Answers
4
public static void lockOrientation(Activity a) {
if (a.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
public static void unlockOrientation(Activity a) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}

Alexander Kulyakhtin
- 47,782
- 38
- 107
- 158
-
1This answer will give the wrong behaviour if the device is in reverse-portrait or reverse-landscape orientation. – Dan Hulme Nov 16 '12 at 11:19
-
@Dan Hulme That's true I've discovered that recently, too. I haven't yet thought how to improve the answer do you know? – Alexander Kulyakhtin Nov 16 '12 at 12:28
-
I proposed an edit to the question which is stemmed off of this question: http://stackoverflow.com/questions/8388846/how-to-detect-exact-orientation-of-device-in-froyo – Sababado Jan 06 '13 at 16:04