0

I'm having a problem with Android 2.2 and screen orientation. I have checkbox on my interface that when checked, the orientation must be locked on the current orientation, so I did the following code:

Activity a = (Activity) getContext();

if (isChecked) {
    if (getResources().getConfiguration().orientation == configuration.ORIENTATION_LANDSCAPE)
        a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
    a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}

The problem with this is that if I turn upside down the device, my screen will rotate to that, and when I click on my "lock orientation", getResources().getConfiguration().orientation will return SCREEN_ORIENTATION_PORTRAIT and my code will lock the orientation to SCREEN_ORIENTATION_PORTRAIT and the interface will be upside-down.

I can see that on Gingerbread (http://developer.android.com/reference/ android/R.attr.html#screenOrientation), there is a reversePortait and reverseLandscape to solve this, but I really need this code to run on 2.2, so is there anyway to set the screen to upside down? Or is there any other way to lock the rotation?

Paulo Cesar
  • 2,250
  • 1
  • 25
  • 35
  • Have you fixed this? I have a solution that works a bit better than yours here: http://stackoverflow.com/questions/6599770/screen-orientation-lock It's still not fully functional for Honeycomb as portrait gets reversed. – Michael Pardo Jul 07 '11 at 00:22

1 Answers1

0

Use ScreenRotation. ScreenOrientation is deprecated and does not know about upside-down portrait orientation.

Marcus Wolschon
  • 2,550
  • 2
  • 22
  • 28
  • Can you post a code example or point to the doc for this? I don't see any constants for rotation. – Michael Pardo Jul 07 '11 at 00:21
  • http://developer.android.com/reference/android/view/Display.html#getOrientation%28%29 – Marcus Wolschon Jul 07 '11 at 08:47
  • Display.getOrientation () Since: API Level 1 This method is deprecated. use getRotation() – Marcus Wolschon Jul 07 '11 at 08:47
  • ScreenRotation is bad because some devices have "natural orientation" to landscape, while others have portrait, so while ROTATION_90 is landscape on a Galaxy Tab, it will be portrait on a Xoom. – Paulo Cesar Oct 27 '11 at 15:37
  • Yes, you have to check the natural orientation too to use rotation as the device-manufacturer defines what the natural rotation 0° is for his device. This may help: http://softteco.blogspot.com/2011/08/universal-way-to-detect-landscape-mode.html – Marcus Wolschon Feb 01 '12 at 10:59