Tushar patel
Tushar patel

Reputation: 3407

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.

Upvotes: 52

Views: 79465

Answers (7)

Nicolas Bodin
Nicolas Bodin

Reputation: 1581

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.

Upvotes: 32

Patrick
Patrick

Reputation: 1737

I would like to 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 (e.g. 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}}"/>

Upvotes: 6

EvZ
EvZ

Reputation: 12169

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;

// Width (in xamarin.forms units)
var xamarinWidth = width / mainDisplayInfo.Density;

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

// Screen density
var density = mainDisplayInfo.Density;

Upvotes: 80

Igor Monteiro
Igor Monteiro

Reputation: 953

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);
            }

Upvotes: 1

Sparsha Bhattarai
Sparsha Bhattarai

Reputation: 703

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;
}

Upvotes: 3

Khoa Tran
Khoa Tran

Reputation: 131

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/

Upvotes: 8

Parth Patel
Parth Patel

Reputation: 3997

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.

Upvotes: 85

Related Questions