Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Landscape and Portrait switched in Android #10

Open
Bracefor opened this issue Feb 1, 2018 · 3 comments
Open

Landscape and Portrait switched in Android #10

Bracefor opened this issue Feb 1, 2018 · 3 comments

Comments

@Bracefor
Copy link

Bracefor commented Feb 1, 2018

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;
                    }
                }
            };
        }
    }
}
wcoder added a commit that referenced this issue Feb 5, 2018
@wcoder wcoder added the bug label Feb 5, 2018
@wcoder
Copy link
Owner

wcoder commented Feb 5, 2018

@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?

@Huaba93
Copy link

Huaba93 commented May 11, 2020

@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.

@Huaba93
Copy link

Huaba93 commented May 12, 2020

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants