I am trying to rewrite an app that I wrote for iOS. I was going to write an android version but thought It'd be better to make this the opportunity to use Xamarin.Forms. Doing it one page at a time, now I'm stuck on a page where I need to get the screen's width and height. Does anyone know the equivalent of iOS' View.Frame.Width in Xamarin.Forms?
6 Answers
Update: You can use Xamarin.Essentials nuget package for this and many other purposes.
var width = DeviceDisplay.MainDisplayInfo.Width;
var height = DeviceDisplay.MainDisplayInfo.Height;
Old answer:
There is an easy way to get the screen's width and height in Xamarin.Forms
and access it globally from everywhere in your app. I'd do something like this:
1. Create two public members in your App.cs:
public static class App
{
public static int ScreenWidth;
public static int ScreenHeight;
...
}
2. Set the values in your MainActivity.cs
(Android) or your AppDelegate.cs
(iOS):
Android:
protected override void OnCreate(Bundle bundle)
{
...
App.ScreenWidth = (int)Resources.DisplayMetrics.WidthPixels; // real pixels
App.ScreenHeight = (int)Resources.DisplayMetrics.HeightPixels; // real pixels
// App.ScreenWidth = (int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density); // device independent pixels
// App.ScreenHeight = (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density); // device independent pixels
...
}
iOS:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
...
App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;
App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
...
}

- 4,817
- 5
- 42
- 53
-
@Julien Before Xamarin.Forms version 1.3.x it was. An even more accurate way is described in our book on http://xforms-kickstarter.com/ under **Referring to the screen size**. – kaolick Jan 16 '15 at 13:30
-
On Android i suggest use: var pixels = Resources.DisplayMetrics.WidthPixels; // dip pixels var scale = Resources.DisplayMetrics.Density; int dps = (int)((pixels - 0.5f) / scale); App.ScreenWidth = dps; pixels = Resources.DisplayMetrics.HeightPixels; // dip pixels dps = (int)((pixels - 0.5f) / scale); App.ScreenHeight = dps; // dip pixels – Maury Apr 10 '15 at 12:48
If you are using xamarin forms, then you can find width and height of current screen in portable class library(pcl) like below.
For Width you use this in pcl,
Application.Current.MainPage.Width
For height you can do this in pcl,
Application.Current.MainPage.Height

- 3,279
- 3
- 27
- 30
-
I am getting the runtime exception " Object reference not set to an instance of an object.". – Joyce Babu Nov 09 '16 at 06:10
-
1this gives you width and height you define. not the actual screen device sizes. you can add in xaml width=5000 and it will return you 5000. you must go native to get actual screen sizes. – Emil Jan 15 '17 at 14:29
There isn't currently a way from Xamarin.Forms itself but we have it implemented as PCL compatible interface in Xamarin.Forms.Labs which you can get from NuGet or source code from GitHub.
https://github.com/XForms/Xamarin-Forms-Labs
IDevice has IDisplay property with the information; height, width, pixel density for X & Y and couple of extension methods to calculate sized in inches.
Sample page for getting information from the device:
#region Display information
var display = device.Display;
var displayFrame = new Frame();
if (display != null)
{
displayFrame.Content = new StackLayout()
{
Children =
{
new Label() { Text = display.ToString() },
new Label() { Text = string.Format("Screen width is\t {0:0.0} inches.", display.ScreenWidthInches()) },
new Label() { Text = string.Format("Screen height is\t {0:0.0} inches.", display.ScreenHeightInches()) },
new Label() { Text = string.Format("Screen diagonal size is\t {0:0.0} inches.", display.ScreenSizeInches()) }
}
};
}
else
{
displayFrame.Content = new Label() { TextColor = Color.Red, Text = "Device does not contain display information." };
}
stack.Children.Add(displayFrame);
#endregion
Creating an exact inch-by-inch frame on all platforms regardless of display properties:
public class AbsoluteLayoutWithDisplayInfoPage : ContentPage
{
public AbsoluteLayoutWithDisplayInfoPage(IDisplay display)
{
this.Title = "Absolute Layout With Display Info";
var abs = new AbsoluteLayout();
var inchX = display.WidthRequestInInches(1);
var inchY = display.HeightRequestInInches(1);
var originX = display.WidthRequestInInches(display.ScreenWidthInches() / 2);
var originY = display.HeightRequestInInches(display.ScreenHeightInches() / 2);
abs.Children.Add(new Label() { Text = "1\"x\"1\" blue frame" });
abs.Children.Add(new Frame()
{
BackgroundColor = Color.Navy,
},
new Rectangle(originX - inchX/2, originY - inchY/2, inchX, inchY));
abs.Children.Add(new Frame()
{
BackgroundColor = Color.White
},
new Rectangle(originX - inchX/16, originY - inchY/16, inchX/8, inchY/8));
this.Content = abs;
}
}
To get to the device info either set your DI resolver or use a static container. All 3 platforms have a singleton device calls with static CurrentDevice property:
resolverContainer.Register<IDevice>(t => WindowsPhoneDevice.CurrentDevice)
resolverContainer.Register<IDevice>(t => AppleDevice.CurrentDevice)
resolverContainer.Register<IDevice>(t => AndroidDevice.CurrentDevice)

- 5,234
- 1
- 16
- 25
-
Thanks @SKall , I'm not doing my app the PCL way - but I'll sure take a look and see if i can get some ideas. Thank you! – dmc Jul 04 '14 at 23:27
-
The code of course works whether using shared code or PCL. On shared code you don't need DI, just use compiler flags to get the current device. – SKall Jul 05 '14 at 23:05
-
-
Hi @SKall, I finally tried this this morning but encountered something weird. display.ScreenWidthInches() always returns 640 on iphone, which doesn't make sense because I know the width should only be over 300. What I am trying to do is fit in 4 buttons within the phone's frames (it should be button's width = screen width / 4). Am I missing something? Thoughts? – dmc Jul 29 '14 at 22:37
-
Can you tell me what the PhoneType is (Phone.Version)? If it is iPhone then IDevice should be of type Phone at runtime. You could also call in the static method of AppleDevice.GetSystemProperty("hw.machine"); to get the hardware version string. That would tell me if there is a problem identifying the device. – SKall Jul 30 '14 at 00:57
-
-
The display width for iPhone 5 is 640 pixels. ScreenWidthInches() should return 2.31 (inches). – SKall Jul 30 '14 at 03:11
-
Hi @SKall, Really sorry for bothering you mate. Quick question , coz I'm a bit lost here. Before I implemented this, my buttons' widthRequest was 75. I had 4 buttons fit in the screen perfectly with 5 as button gaps. Now, from 640, how do i convert that to make it work like before? When I say display.WudthRequestInInches(1), I am getting 163. How do I convert display.Width? Is there any method I am missing here? – dmc Jul 30 '14 at 03:32
-
You would get the actual width in inches (2.31) and divide that by 4. Then call in WidthRequestInInches(2.31 / 4) which will give you the 1/4 length screen width request to use with Xamarin.Forms. Deduct any padding you want to apply. You can also look at the sample page here: https://github.com/XForms/Xamarin-Forms-Labs/blob/master/samples/Xamarin.Forms.Labs.Sample/Pages/Services/AbsoluteLayoutWithDisplayInfoPage.cs – SKall Jul 30 '14 at 16:00
-
Awesome! That worked @SKall, thanks!... I actually was able to make my buttons fit in the screen using the FillAndExpand horizontal options (but the width aren't even - like some are wider and some are thinner...which actually looked better than i expected), BUT still needed the actual screen width so I could calculate for the height of my buttons to make it proportion to their width. Thanks mate! – dmc Jul 30 '14 at 22:24
Recipe
Create a new Xamarin.Android application named ScreenSize. Edit Main.axml so that it contains two TextViews:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Screen Width:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/screenWidthDp" />
<TextView
android:text="Screen Height:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/screenHeightDp" />
</LinearLayout>
Edit Activity1.cs, change the code in OnCreate to the following:
![enter image description here][1] private int ConvertPixelsToDp(float pixelValue) { var dp = (int) ((pixelValue)/Resources.DisplayMetrics.Density); return dp; }
Run the application. Depending on the device, it will display the screen height and width. The following screen shot is from a Galaxy Nexus:
[image link] https://i.stack.imgur.com/TQhba.png

- 11
- 4
In your App file define a public static variable
public static Size ScreenSize;
You should initialise the variable both in IOS and Android before calling the App constructer. For IOS, in FinishedLaunching method
App.ScreenSize = new Size(UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
and For Android, in OnCreate function
App.ScreenSize = new Xamarin.Forms.Size((int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density), (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density));

- 1,051
- 11
- 17
Updating for future developers trying to do this that either haven't looked or missed it. The Page class inherits from VisualElement, which now has two properties (which happen to be bindable for use in XAML):
Width - Gets the current rendered width of this element. This is a read-only bindable property. Height - Gets the current rendered height of this element. This is a read-only bindable property.
in your page codebehind you would just do something like:
var myWidth = this.Width;
var myHeight = this.Height;
if you needed to know if it changes, use the SizeChanged event:
public MyPage()
{
SizeChanged += OnSizeChanged;
}
private void OnSizeChanged(object sender, EventArgs e)
{
var myWidth = this.Width;
var myHeight = this.Height;
}

- 570
- 4
- 14