Screen Size for iOS, Android and Windows Phone from C# with Xamarin in Shared Code.

To get the screen size from either iOS, Android, or Windows Phone you could use the following examples:

int intHeight = (int)Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density;
int intWidth = (int)Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density;
int intHeight = UIScreen.MainScreen.Bounds.Height; 
int intWidth = UIScreen.MainScreen.Bounds.Width;
int intHeight = Window.Current.Bounds.Height; 
int intWidth = Window.Current.Bounds.Width;

To get screen size from any one of the platforms in a shared code base you could include conditional compile directives as follows:

int intHeight = 0;
int intWidth = 0;
#if __ANDROID__
	intHeight = (int)Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density;
	intWidth = (int)Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density;
#end if 
#if __IOS___
	intHeight = UIScreen.MainScreen.Bounds.Height; 
	intWidth = UIScreen.MainScreen.Bounds.Width;
	intHeight = Window.Current.Bounds.Height; 
	intWidth = Window.Current.Bounds.Width;

Leave a Reply

Your email address will not be published. Required fields are marked *