Reputation: 1645
This is an image from a sample application I created in an attempt to calculate the screen size.
What I am trying to accomplish is to determine the actual (usable) area of the screen (the white portion of the screen in the image). The application I am working on has a requirement to be able to scroll a screen at a time when a certain key is pressed (the devices we are deploying to have physical keyboards attached). Also, the devices we will be deploying to have the Android OS.
In the sample application, I am obtaining the Height, Width, and Density values using the Xamarin Essentials package. I am getting the Navigation Bar Height value using the following code that I have found in several places on the web (in the Android project):
var resourceId = Context.Resources.GetIdentifier("navigation_bar_height", "dimen", "android");
var navigationBarHeight = Context.Resources.GetDimensionPixelSize(resourceId);
The questions are:
I attempted to calculate the size of the usable portion of the screen by using:
(screenHeight - navigationBarHeight) / density
This does not seem to be correct though.
What am I missing?
* UPDATE *
Looking at the answer to another Stack Overflow question (here), the value for navigation bar height is the bar at the bottom. This answer also makes mention of the status bar (which I am not currently taking into account but probably should be).
Also, reading another document page (here), I should be able to get the height of what I was calling the navigation bar (the blue bar in the original image). I cannot figure out where I should make this call though. I tried placing it in the OnElementChanged
override of my custom navigation page renderer. When placed here, the value is always 0. I am guessing that this is because the bar has not actually been rendered yet. If this is true, where should the call to this method be placed?
Upvotes: 1
Views: 1003
Reputation: 58
Late to the party, but here is my contribution. As you can see from other answers, there are several ways to get the screen size, however there is a difference between the actually screen size and what I call the effective screen size. For example, on Android, if you adjust the Zoom setting you are changing the effective screen size. Here is and example in Xamarin Forms:
If you put the following code in you view code behind you will see a difference between the actual screen size and the effective screen size. The OnSizeAllocated parameters appear to contain the effective screen size while the DeviceDisplay.MainDisplayInfo.Width & Height appear to contain the actual display size. Output is below the code snippet.
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var orientation = mainDisplayInfo.Orientation;
var rotation = mainDisplayInfo.Rotation;
var widthx = mainDisplayInfo.Width;
var heightx = mainDisplayInfo.Height;
var densityx = mainDisplayInfo.Density;
Log($"STATS: Orientation={orientation}
Rotation={rotation}
Effective Width={width}
Effective Height={height}
Width={widthx}
Height={heightx}
Density={densityx}");
}
Output:
Notice that with the "normal" zoom level the effective and actual screen size are the same, and the density is 1.
Zoom Huge : STATS: Orientation=Landscape Rotation=Rotation90 Effective Width=853.333333333333 Effective Height=533.333333333333 Width=1280 Height=800 Density=1.5
Zoom Large : STATS: Orientation=Landscape Rotation=Rotation90 Effective Width=975.238095238095 Effective Height=609.52380952381 Width=1280 Height=800 Density=1.3125
Zoom Med : STATS: Orientation=Landscape Rotation=Rotation90 Effective Width=1137.77777777778 Effective Height=711.111111111111 Width=1280 Height=800 Density=1.125
Zoom Normal: STATS: Orientation=Landscape Rotation=Rotation90 Effective Width=1280 Effective Height=800 Width=1280 Height=800 Density=1
Zoom Small : STATS: Orientation=Landscape Rotation=Rotation90 Effective Width=1462.85714285714 Effective Height=914.285714285714 Width=1280 Height=800 Density=0.875
Upvotes: 0
Reputation: 442
Xamarin.Essentials.DeviceDisplay.MainDisplayInfo.Height Xamarin.Essentials.DeviceDisplay.MainDisplayInfo.Width
Upvotes: 0
Reputation: 144
There is one Override method Provide by xamarin.Forms Below Method You can write in PageXaml.cs file
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
}
Upvotes: 1
Reputation: 18861
You can get the size of screen in specific platform and get the value in share project.
public static double ScreenWidth;
public static double ScreenHeight;
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Forms.SetFlags("CollectionView_Experimental");
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
App.ScreenWidth = Resources.DisplayMetrics.WidthPixels/Resources.DisplayMetrics.Density;
App.ScreenHeight =Resources.DisplayMetrics.HeightPixels/Resources.DisplayMetrics.Density;
LoadApplication(new App());
}
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
//...
App.ScreenWidth = UIScreen.MainScreen.Bounds.Width;
App.ScreenHeight = UIScreen.MainScreen.Bounds.Height;
//...
}
// do something you want
var height = App.ScreenHeight;
var width = App.ScreenWidth;
Upvotes: 0