0

Essentially, I want to find the value that would be returned by Display#getRotation() if the device were oriented in straight up in portrait mode, but without assuming that the user is holding their device this way. The reason I want to do this is that I have a camera activity which should always run in portrait mode, and I need to set the orientation of the camera appropriately so that the image appears in the same orientation as reality, but I can't guarantee that the device is in any particular orientation when this activity starts and the camera is initialized, not only because users are unpredictable, but also because they may be entering the camera from a landscape-mode activity, if they are using a tablet.

I saw https://stackoverflow.com/a/9888357/3634598, but I was unsatisfied with this solution because it makes the assumption that the user is holding the device in the same orientation as the window, which is faulty in my case since I am locking the orientation to portrait.

It's frustrating to me that Android would have this concept of a natural orientation for which developers are supposed to compensate, with no context-free way to get at it.

Community
  • 1
  • 1
Adam
  • 11
  • 2
  • 1
    **" it makes the assumption that the user is holding the device in the same orientation as the window"** : That's not how I understand it. It compares the current orientation (i.e., the way the user is holding it) with the various angles of 0, 180, 90, 270 degrees. If the current orientation (portrait or landscape) corresponds to 0 or 180 degrees then that is the natural orientation. If the current orientation corresponds to 90 or 270 degrees then the other orientation is the natural orientation. – Squonk May 20 '14 at 20:33
  • @Squonk >If the current orientation (portrait or landscape) corresponds to 0 or 180 degrees then that is the natural orientation. This is the case if you allow the OS to change the activity orientation in response to reorientation of the device, but in the case that you lock the orientation of the activity to ie. portrait or landscape, then the `Configuration#orientation` will be always be that and this check is essentially useless. – Adam May 20 '14 at 20:53
  • Have you actually tested it with and without locking your orientation? Also, can the users enter the 'locked' camera activity from 3rd party apps or only from other activities in your own app? If so (and you have non-locked activities) then why not use that code in one of those and hold the 'natural' orientation as a global variable? You could even extend the `Application` class which is created before any other app components and has no UI so therefore has no concept of locking of orientation, i.e., you should always get an accurate result. – Squonk May 20 '14 at 21:58

1 Answers1

0

Instead of getting natural display orientation, you can force an activity to use portrait only orientation.

In code inside your activity:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

or in manifest inside activity tag:

<activity android:screenOrientation="portrait" ... />
Prizoff
  • 4,486
  • 4
  • 41
  • 69
  • I am already doing this. The reason I need to get the natural orientation is *because* I'm doing this. – Adam May 20 '14 at 20:29