Density of screen in iOS and Universal WIndows App
Asked Answered
N

2

15

Hi I need to know the density of screen in windows 10/8/8.1 and iOS. I got screen density in Android using DisplayMetrics but I find no such option/property available in UWP and iOS. So Is there any property through which I can get screen density in UWP and IOS.

Natch answered 5/1, 2017 at 16:6 Comment(8)
Take a look at this post https://mcmap.net/q/822012/-how-wide-is-the-master-page-of-a-master-detail-page-in-xamarin-forms ?Mclane
This is also a nice read - https://mcmap.net/q/822013/-how-do-gui-developers-deal-with-variable-pixel-densitiesMclane
@MatthewRegul but how to get density of screen in windows case (8,8.1,10)Natch
That's the real question, I still need to figure that out myself. I'll reply if I find anything.Mclane
@MatthewRegul ok thanks.Natch
Hopefully the code below works for you.Mclane
Sonali, were you able to solve your issue?Mclane
@MatthewRegul yes.Natch
M
42

UPDATE - June 8th 2018

Xamarin has released Xamarin.Essentials!

Xamarin.Essentials provides developers with cross-platform APIs for their mobile applications.

Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created. https://github.com/xamarin/Essentials


You can now query the device's display information without having to manage all of the individual platform code pieces.

Device Display Information - https://learn.microsoft.com/en-us/xamarin/essentials/device-display

How-To:

Add a reference to Xamarin.Essentials in your class:

using Xamarin.Essentials;

The following information is exposed through the API:

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

Useful Links:









Original Post (Before Xamarin.Essentials existed)

Below, I'm storing the specific OS's screen details in static vars, located in the Xamarin Forms App class

These all appear to work in my testing.

(copy/pasted from my github page - https://github.com/mattregul/Xamarin_Screensize)

Android (MainActivity.cs) - Jump to Github Page

// Store off the device sizes, so we can access them within Xamarin Forms
// Screen Width = WidthPixels / Density
// Screen Height = HeightPixels / Density

App.DisplayScreenWidth  = (double)Resources.DisplayMetrics.WidthPixels / (double)Resources.DisplayMetrics.Density;
App.DisplayScreenHeight = (double)Resources.DisplayMetrics.HeightPixels / (double)Resources.DisplayMetrics.Density; 
App.DisplayScaleFactor  = (double)Resources.DisplayMetrics.Density;

iOS (AppDelegate.cs) - Jump to Github Page

// Store off the device sizes, so we can access them within Xamarin Forms
App.DisplayScreenWidth  = (double)UIScreen.MainScreen.Bounds.Width;
App.DisplayScreenHeight = (double)UIScreen.MainScreen.Bounds.Height;
App.DisplayScaleFactor  = (double)UIScreen.MainScreen.Scale;

Note In all Windows examples screensize is the root project namespace

UWP (App.xaml.cs) - Jump to Github Page

// You decided which is best for you...
//  Do you want Size of the App's View
//      or
//  Do you want the Display's resolution 
// ######################################

// Size of App's view
screensize.App.DisplayScreenHeight = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Height;
screensize.App.DisplayScreenWidth = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Width;

// Size of screen's resolution
//screensize.App.DisplayScreenWidth = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels;
//screensize.App.DisplayScreenHeight = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;

// Pixels per View Pixel
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/design-and-ui-intro#effective-pixels-and-scaling
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/screen-sizes-and-breakpoints-for-responsive-design
screensize.App.DisplayScaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;

Windows Phone 8.1 (App.xaml.cs) - Jump to Github Page

// Size of App's view
screensize.App.DisplayScreenHeight = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Height;
screensize.App.DisplayScreenWidth = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Width;

// Pixels per View Pixel
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/design-and-ui-intro#effective-pixels-and-scaling
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/screen-sizes-and-breakpoints-for-responsive-design
screensize.App.DisplayScaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;

Windows 8.1 (App.xaml.cs) - Jump to Github Page

// Size of App's view
screensize.App.DisplayScreenHeight = Window.Current.Bounds.Height;
screensize.App.DisplayScreenWidth = Window.Current.Bounds.Width;
screensize.App.DisplayScaleFactor = 1; // No scaling here?  If you find a scaling for Windows 8.1, please let me know :)

Xamarin Forms Page (App.cs) - Jump to Github Page

namespace screensize
{
    public class App : Application
    {
        public static double DisplayScreenWidth = 0f;
        public static double DisplayScreenHeight = 0f;
        public static double DisplayScaleFactor = 0f;

        public App()
        {

            string ScreenDetails = Device.OS.ToString() + " Device Screen Size:\n" +
                $"Width: {DisplayScreenWidth}\n" +
                $"Height: {DisplayScreenHeight}\n" +
                $"Scale Factor: {DisplayScaleFactor}";

            // The root page of your application
            var content = new ContentPage
            {
                Title = "Xamarin_GetDeviceScreensize",
                Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        new Label {
                            HorizontalTextAlignment = TextAlignment.Center,
                            FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
                            Text = ScreenDetails
                        }
                    }
                }
            };

            MainPage = new NavigationPage(content);
        }

    }
}
Mclane answered 6/1, 2017 at 16:55 Comment(0)
M
1

This list lists the ios device dpi information. You can also use GBDeviceInfo thrid-party tool to get the screen density for IOS device like this [GBDeviceInfo deviceInfo].displayInfo.pixelsPerInch;

IOS:

   iPhones
iPhone 1/3G/3GS           320 x  480    163 ppi
iPhone 4/4S               640 x  940    326 ppi
iPhone 5/5C/5S            640 x 1136    326 ppi
iPhone 6/6S               750 x 1334    326 ppi
iPhone 6 Plus/6S Plus    1080 x 1920    401 ppi
  Without downsampling:  1242 x 2208    461 ppi

Except for the 6th generation (= 5th) iPod touch,
all the models are equal to their iPhone counterparts

iPads
iPad 1/2                  768 x 1024    132 ppi
iPad 3/4/Air/Air 2       1536 x 2048    264 ppi
iPad Mini 1               768 x 1024    163 ppi
iPad Mini 2/3/4          1536 x 2048    326 ppi
iPad Pro                 2737 x 2048    264 ppi

Apple Watches
Apple Watch 38 mm         272 x  340    326 ppi
Apple Watch 42 mm         312 x  390    326 ppi

I found API in the UWP to retrieve the device dpi. Windows.Graphics.Display.DisplayInformation. You can try it.

Malindamalinde answered 6/1, 2017 at 7:58 Comment(9)
I used (double)UIScreen.MainScreen.Scale; it always returns one. Why??Natch
how can i get xdpi and ydpi in ios?Natch
By default, The value of MainScreen.Scale is always one because of the original displaying settings of your device.Malindamalinde
ok then how can i get xdpi and ydpi in ios? @Nico Zhu - MSFTNatch
Please try to use [GBDeviceInfo deviceInfo].displayInfo.pixelsPerInch api in your ios project.Malindamalinde
couldn't find this package in nuget package manager. @Nico Zhu - MSFTNatch
The GBDeviceInfo thrid-party tool is only available in ios project coding by object-c.Malindamalinde
but how will I use this in my portable xamarin projectNatch
Let us continue this discussion in chat.Malindamalinde

© 2022 - 2024 — McMap. All rights reserved.