How to restrict device orientation to Portrait in entire app?

I have built a dealer management system app using Intersoft Crosslight ,
I have only 3 activities in it, rest all screens in the app have fragments, in which i have written the following line to restrict the screen in portrait mode :

this.Activity.RequestedOrientation = ScreenOrientation.Portrait; (in OnViewInitialized() method)

However, the individual screens are restricted in portrait mode, but when I tilt the phone(landscape mode) and then navigate from one screen(which is in portrait mode) to other screen the app crashes.
Please suggest any solution, any other approach to restrict the entire app in portrait mode is also appreciated.

Answers

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    edited August 2018

    @ajinkyas

    First moving this thread to the Xamarin.Android category.

    Instead of putting that line of code in OnViewInitialized(), which may be too late to set that property, instead add it to the Activity attribute on each Activity class, e.g.:

    [Activity(Label = "AndroidPortrait", MainLauncher = true, Icon = "@mipmap/icon", 
                       ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
    public class MainActivity : Activity
    {
        ...
    } 
    

    This will add the restriction to the generated AndroidManifest.xml file and thus the RequestedOrientation property will be set when the activity is instantiated. You do need to do this for every activity you have that you want to restrict to portrait. The Activity attribute basically adds an <Activity> xml element to the AndroidManifest.xml file at build time, and populates the <Activity> element with the passed in arguments.

  • ajinkyasajinkyas Member ✭✭
    edited August 2018

    @JGoldberger Thank you, but this does not resolve my issue.
    First of all i have fragments not activities and i tried to do the same as you mentioned on fragment classes but it did not work, Please help me out.
    If you want some code sample

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @ajinkyas

    Did you try putting that code in the Fragment's OnCreateView override instead of OnViewInitialized?

  • ajinkyasajinkyas Member ✭✭

    @JGoldberger I tried it, but the issue still persists.

  • BhautikBhautik Member ✭✭✭
    edited August 2018

    You can do that using the MessagingCenter.
    In Android I achieved this by doing the following:
    In your MainActivity.cs:
    MessagingCenter.Subscribe<YourClass>(this, "AllowLandscape", sender => { RequestedOrientation = ScreenOrientation.Landscape; }); //forces app to portrait mode after closing a Page containing only a Plot MessagingCenter.Subscribe<YourClass>(this, "PreventLandscape", sender => { RequestedOrientation = ScreenOrientation.Portrait; });

    In your Page:
    MessagingCenter.Send(this,"AllowLandscape"); MessagingCenter.Send(this, "PreventLandscape");

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @ajinkyas

    I tried to reproduce the described issue but I cannot. Would it be possible for you to share a very simple test project that demonstrates the issue, ideally without using Intersoft Crosslight and no sensitive code/data?

Sign In or Register to comment.