Manthiram
Manthiram

Reputation: 171

find device orientation, not app orientation using xamarin forms

I am using xamarin forms for my app. Bascially My app need portrait mode orientation. But When i click on the button on my page, I need to find whether my device is on portrait or landscape. But my app orientation is always showing portrait mode. MainActivity.cs:

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Content;
using Xamarin.Forms;
using Plugin.CurrentActivity;

namespace App2.Droid
{
    [Activity(Label = "App2", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            if (!isPad(this))
            {
                RequestedOrientation = ScreenOrientation.Portrait;
            }
            else
            {
                RequestedOrientation = ScreenOrientation.Portrait;
            }
            CrossCurrentActivity.Current.Activity = this;
            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }

        public static bool isPad(Context context)
        {
            return (context.Resources.Configuration.ScreenLayout & Android.Content.Res.ScreenLayout.SizeMask) >= Android.Content.Res.ScreenLayout.SizeLarge;
        }

        public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
        {
            base.OnConfigurationChanged(newConfig);

            switch (newConfig.Orientation)
            {
                case Android.Content.Res.Orientation.Portrait:
                    switch (Device.Idiom)
                    {
                        case TargetIdiom.Phone:
                            LockRotation(Orientation.Vertical);
                            break;
                        case TargetIdiom.Tablet:
                            LockRotation(Orientation.Horizontal);
                            break;
                    }
                    break;
                case Android.Content.Res.Orientation.Landscape:
                    switch (Device.Idiom)
                    {
                        case TargetIdiom.Phone:
                            LockRotation(Orientation.Vertical);
                            break;
                        case TargetIdiom.Tablet:
                            LockRotation(Orientation.Horizontal);
                            break;
                    }
                    break;
            }
        }
        private void LockRotation(Orientation orientation)
        {
            switch (orientation)
            {

                case Orientation.Vertical:
                    RequestedOrientation = ScreenOrientation.Portrait;
                    break;
                case Orientation.Horizontal:
                    RequestedOrientation = ScreenOrientation.Landscape;
                    break;

            }
        }


    }
}

TestPage.xaml.cs:

private void btnsubmit_Clicked(object sender, EventArgs e)
        {
            lblText.Text = CrossDeviceOrientation.Current.CurrentOrientation.ToString();
        }

But it is showing always portrait(App Orientation). But I need device orientation.

Please help me to resolve this issue

enter image description here

Upvotes: 0

Views: 773

Answers (1)

Leon Lu
Leon Lu

Reputation: 9234

You can use Xamarin.Essentials: Device Display Information to achieve it.

Here is my code, when device change the Orientation, will push a Alert to show the device Orientation.

  public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            DeviceDisplay.MainDisplayInfoChanged += OnMainDisplayInfoChanged;
        }

        private void OnMainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
        {
            // throw new NotImplementedException();
            DisplayAlert("Info", e.DisplayInfo.Orientation.ToString() + " ", "OK"); 
        }


    }

Here is running GIF.

enter image description here

Upvotes: 1

Related Questions