Bug 5836 - How to get and compare orientation?
Summary: How to get and compare orientation?
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-06-24 10:24 UTC by Maxim
Modified: 2012-09-11 09:40 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Maxim 2012-06-24 10:24:12 UTC
Set orientation is simple - just to call SetRequestedOrientation with desirable value.

1. How to get it?
But how to get orientation value, for example, under activity's onCreate event?
I tried to rotate Android emulator window - any times this.RequestedOrientation was equal to -1.

2. How to compare it correctly?
My final task is to set up desirable activity layout depends on screen orientation. So onCreate I check:
if (this.RequestedOrientation == ScreenOrientation.Portrait)
Unfortunately, compiler said that left side is int, right side is Android.Content.PM.ScreenOrientation. Mmmm, ok, I tried:
if (this.RequestedOrientation == (int)ScreenOrientation.Portrait)
It was compiled, but question #1 appear about current RequestedOrientation value.
Comment 1 Maxim 2012-06-27 13:35:28 UTC
I successfully upgraded my project from Mono for Android 4.0.6 to 4.2.3 according responses to issues #5281, #5319 and question #2 disappeared.

Please let me know why emulator could always return unspecified screen orientation.
Comment 2 Jonathan Pryor 2012-09-11 09:40:51 UTC
> 1. How to get [screen orientation]

Activity.RequestedOrientation is "cached" information: it's either the requested orientation as specified in AndroidManifest.xml (e.g. ActivityAttribute.ScreenOrientation), or the last value provided to Activity.SetRequestedOrientation(). If you haven't done either of those, there is no requested orientation, hence the -1 value.

I believe what you actually want is the _current_ screen orientation, not the requested orientation. You can obtain this from activity.WindowManager.DefaultDisplay.Rotation, which will return a SurfaceOrientation enum value. (Alas, this isn't a ScreenOrientation value; it's possible that the Display.Orientation property can be cast to a ScreenOrientation value, but I haven't tested this.)

> 2. How to compare it correctly?

The underlying value is an int, so you can cast the right-hand side to an int, or the left-hand side to an enum:

    if (this.RequestedOrientation == (int) ScreenOrientation.Portrait)

However, as per (1), that doesn't mean that the value is meaningful or what you wanted. I suspect what you want is the current screen orientation value, not the requested one, as described above.

FYI, asking questions on stackoverflow.com may result in faster answers.