Get current screen width in xamarin forms

I am creating one Xamarin Forms project and I got stuck in finding the current screen width according to the different device.

I know how it can be done in android and ios but need a way to find the width in portable.

Answers


You can try this Application.Current.MainPage.Width in my case this worked and i am able to find the device width in portable project itself.


Common code (in App.xaml.cs)

public static int ScreenHeight {get; set;}
public static int ScreenWidth {get; set;}

Android part (in MainActivity.cs, in the OnCreate method)

App.ScreenHeight = (int) (Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);
App.ScreenWidth = (int) (Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);

iOS part (in AppDelegate.cs, in the FinishedLaunching method)

App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;

So App.ScreenHeight and App.ScreenWidth will be initialized when the App will be launched, then you will be able to use them anywhere in the common code.


I know it is an old thread but worth mentioning that recently Xamarin.Essentials NuGet pakage was released and there is a useful class DeviceDisplay in there that should handle this task for you.

The documentation can be found here.

Usage example:

// Get Metrics
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;

// Orientation (Landscape, Portrait, Square, Unknown)
var orientation = mainDisplayInfo.Orientation;

// Rotation (0, 90, 180, 270)
var rotation = mainDisplayInfo.Rotation;

// Width (in pixels)
var width = mainDisplayInfo.Width;

// Height (in pixels)
var height = mainDisplayInfo.Height;

// Screen density
var density = mainDisplayInfo.Density;

You can use the Width property when you are in a ContentPage. For example,

protected override void OnAppearing()
{
    base.OnAppearing();
    double w = this.Width;
}

Note that this is set during the Layout phase. So, you can only use it after the page is initialized, otherwise it is -1.

https://developer.xamarin.com/api/property/Xamarin.Forms.VisualElement.Width/


Another approach would be to use the override "OnSizeAllocated" for your page and then take the height and width according to the changes on the user's screen. You can create a static class to set the height and width of your screen everytime it changes and then access them from this class when needed.

The static class to set and access the device dimension:

public static class ScreenDimensions
{
    public static double Height { get; set; }
    public static double Width { get; set; }
}

Override to obtain the screen dimensions:

protected override void OnSizeAllocated(double width, double height)
{
    base.OnSizeAllocated(width, height);
    ScreenDimensions.Height = height;
    ScreenDimensions.Width = width;
}

I would create a static struct in the shared code, so I can access the screen width/height values easily (as well as doing bindings in xAML)

The struct in the app namespace (eg. in App.xaml.cs):

using System;
using Xamarin.Forms;

namespace YourAppName
{

    ...

    public struct Constant
    {
        public static double ScreenWidth = Application.Current.MainPage.Width;
        public static double ScreenHeight = Application.Current.MainPage.Height;
    }
}

retrieving screen width in C#:

var labelWidth = Constant.ScreenHeight * 0.2;
// then do something with labelWidth

Retrieving screen width in XAML:

<Label Text="Text with insane font size" FontSize="{Binding Source={x:Static local:Constant.ScreenWidth}}"/>

I Guess that the best way is use the xamarin.forms.Device information.

               Size size = Device.Info.PixelScreenSize;

            if (size.Width <= 720 && size.Height <= 1280)
            {
                stkLayoutTop.Margin = new Thickness(5, 30, 10, 0);
            }
            else
            {
                stkLayoutTop.Margin = new Thickness(5, 50, 10, 0);
            }

Need Your Help

Youtube player api with loop

video youtube youtube-javascript-api

I am struggling with setting up the loop for youtube videos using youtube player api.

Installing numpy on Amazon EC2

python numpy amazon-ec2 pip easy-install

I am having trouble installing numpy on an Amazon EC2 server. I have tried using easy_install, pip, pip inside a virtual env, pip inside another virtual env using python 2.7...