Xamarin.Plugin.DeviceOrientation
Xamarin.Plugin.DeviceOrientation copied to clipboard
Landscape and Portrait switched in Android
On iOS, everything is perfectly fine, but as soon as I use this on Android, everything is switched around? When I rotate the device and print out the orientation, it says it is Landscape, when it is actually Portrait?
Here is my custom Image class where the error exists:
using Plugin.DeviceOrientation;
using Plugin.DeviceOrientation.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace Project.Custom
{
public class DSImage : Image
{
public DSImage(int phonePortrait, int phoneLandscape, int tabletPortrait, int tabletLandscape)
{
if (Device.Idiom == TargetIdiom.Phone)
{
if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Portrait || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.PortraitFlipped)
{
System.Diagnostics.Debug.WriteLine("Phone Portrait");
HeightRequest = phonePortrait;
}
else if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Landscape || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.LandscapeFlipped)
{
System.Diagnostics.Debug.WriteLine("Phone Landscape");
HeightRequest = phoneLandscape;
}
}
else if (Device.Idiom == TargetIdiom.Tablet)
{
if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Portrait || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.PortraitFlipped)
{
System.Diagnostics.Debug.WriteLine("Tablet Portrait");
HeightRequest = tabletPortrait;
}
else if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Landscape || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.LandscapeFlipped)
{
System.Diagnostics.Debug.WriteLine("Tablet Landscape");
HeightRequest = tabletLandscape;
}
}
CrossDeviceOrientation.Current.OrientationChanged += (sender, args) =>
{
if (Device.Idiom == TargetIdiom.Phone)
{
if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Portrait || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.PortraitFlipped)
{
System.Diagnostics.Debug.WriteLine("Phone Portrait");
HeightRequest = phonePortrait;
}
else if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Landscape || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.LandscapeFlipped)
{
System.Diagnostics.Debug.WriteLine("Phone Landscape");
HeightRequest = phoneLandscape;
}
}
else if (Device.Idiom == TargetIdiom.Tablet)
{
if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Portrait || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.PortraitFlipped)
{
System.Diagnostics.Debug.WriteLine("Tablet Portrait");
HeightRequest = tabletPortrait;
}
else if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Landscape || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.LandscapeFlipped)
{
System.Diagnostics.Debug.WriteLine("Tablet Landscape");
HeightRequest = tabletLandscape;
}
}
};
}
}
}
@Bracefor, thanks for your report!
I published a new version of the plugin: https://www.nuget.org/packages/Plugin.DeviceOrientation/1.0.7
For the fix, look at this section: https://github.com/wcoder/Xamarin.Plugin.DeviceOrientation#xamarinforms-android
Could you please check this version, and tell me about your results?
@wcoder
This bug still exists. On some Android tablets CrossDeviceOrientation.Current.CurrentOrientation
is reversed. I guess this is because some tablets are in landscape mode by default. We could reproduce this with
- Sony Xperia Tablet Z2 (android 5.1.1)
- Google pixel c (android 8.1.0)
- Google Nexus 10 (android 5.1.1)
I suspect there are many more devices with this problem. we could't reproduce it with emulators.
@wcoder I could also reproduce on a Samsung Galaxy Tab 4
activity.Resources.Configuration.Orientation
returns Landscape
activity.WindowManager.DefaultDisplay.Rotation
returns Rotation180
and is converted by your plugin to DeviceOrientations.PortraitFlipped
but this is wrong
activity.WindowManager.DefaultDisplay
is deprecated since API Level 11, this should really be replaced with a more recent solution