Is there a way to create differnent view for mobile, portrait mode of tab and landscape mode in xamarin forms

harsha111111 121 Reputation points
2021-08-10T04:42:11.797+00:00

I'm working on xamarin forms, I need an application with two types of UI i.e., one for mobile and portrait mode of tablet and other is for landscape mode of tablet

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Kyle Wang 5,531 Reputation points Microsoft External Staff
    2021-08-11T07:04:56.65+00:00

    Hi harsha111111-5189,

    Welcome to our Microsoft Q&A platform!

    To achieve different layouts for portrait mode and landscape mode, you can refer to the document: Device Orientation, which makes it possible to design interfaces using the built-in layouts (StackLayout, AbsoluteLayout, RelativeLayout and Grid) so that they transition gracefully when the device is rotated.

    And if you want to use "landscape" only in tablet, for Android, you can modify the MainActivity as follows to check if is a phone or not.

    protected override void OnCreate(Bundle savedInstanceState)  
    {  
        base.OnCreate(savedInstanceState);  
      
        if (!isPad(this))  
        {  
            RequestedOrientation = ScreenOrientation.Portrait;  
        }  
      
        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 & ScreenLayout.SizeMask) >= ScreenLayout.SizeLarge;  
    }  
    

    Then the "landscape" will be disabled for phone.

    For iOS, to disable the "landscape" on iPhone, just modify the "Supported interface orientations" in info.plist as follows.

    122206-infoplist.png

    Last, make sure Auto-rotate is ON.

    Regards,
    Kyle


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.