0

I am trying to make pictures using the camera be oriented correctly when saved. I tried using the orientation sensor but it can't seem to be calibrated. I wanted to just grab the orientation like this: How can I get the current screen orientation? but it seems that locking your app as a portrait only app means it will always return portrait only.

So how do I get the screens real orientation despite the app being locked in landscape mode?

Community
  • 1
  • 1
shane
  • 415
  • 5
  • 18

2 Answers2

0

If you are trying to get activity orientation, then try this:

Activity.getResources().getConfiguration().orientation

If you want to get android device orientation, then try this :

getResources().getConfiguration().orientation

If you face orientation issue with device camera, then you will try following code snippet to set camera orientation :

if (getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {   
    p.set("orientation", "portrait");
    p.set("rotation",90);
}
if (getResources().getConfiguration().orientation ==Configuration.ORIENTATION_LANDSCAPE)
{                               
    p.set("orientation", "landscape");          
    p.set("rotation", 90);
}
Hemanth
  • 2,717
  • 2
  • 21
  • 29
  • `Activity.getResources().getConfiguration().orientation` and `getResources().getConfiguration().orientation` always return Portrait for me. `IWindowManager windowManager = Context.GetSystemService(Context.WindowService).JavaCast(); var orientation3 = windowManager.DefaultDisplay.Rotation;` Also always returns Rotation0 for me. – shane Feb 09 '15 at 08:03
0

I finally found a solution that worked for me based on this response. Android: Detect Orientation Changed . I made a OrientationListener. Orientation is in degrees and is exactly what I needed.

    internal class MyOrientationListener : global::Android.Views.OrientationListener
{
    private MainActivity _parent;
    public MyOrientationListener(Context context) : base(context)
    {
        _parent = (MainActivity)context;
    }

    public override void OnOrientationChanged(int orientation)
    {
        _parent.GlobalOrientation = orientation;
    }
}

Edit: Note this is for xamarin. I setup this in the MainActivity in CreateBundle(bundle) like this: _orientationListener = new QuarcOrientationListener(this);

Don't forget to .Enable()/.Disable() it in OnPause and OnResume in the activity.

Community
  • 1
  • 1
shane
  • 415
  • 5
  • 18