How does one Programmatically determine the Screen Inch size of a device?

toyinstoyins USMember ✭✭✭

How does one Programmatically determine the Screen Inch size of a device i.e. 5", 5.7", etc.?

Best Answers

  • toyinstoyins US ✭✭✭
    Accepted Answer

    @Amar_Bait, you were right, my screen resolution was set to FHD+(2220x1080), changing to WQHD+ showed the resolution as 1440 x 2960.
    Thanks @Amar_Bait, @JohnHardman, @JoeManke and @ColeX.
    I really appreciate it.

    I used DependencyService within Xamrin.Forms to retrieve the value.

Answers

  • toyinstoyins USMember ✭✭✭

    @JoeManke
    How do I get the inch size from the code below?

    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;

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited June 24

    Screen density means pixels per square inch, and you have the number of pixels. Do the math.

  • toyinstoyins USMember ✭✭✭

    @JoeManke, formula please, if you can?

  • toyinstoyins USMember ✭✭✭
    edited June 25

    @JoeManke , thanks for the prompt response, I tried your formula the result was far out.
    I'm testing with a 5.7" Marshmallow Emulator.

    I'm currently using the Xamarin.Essentials package to retrieve the width, height and density of the Emulator.

    Here are the details.

        var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
    
            // Width (in pixels)
            var width = mainDisplayInfo.Width;
    
            // Height (in pixels)
            var height = mainDisplayInfo.Height;
    
            // Screen density
            var density = mainDisplayInfo.Density;
    
            var i = width / density;
    

    Width = 1440
    Density = 3.21875
    i = 447.378640776699 which is far from 5.7".

    Am I missing something?

  • JohnHardmanJohnHardman GBUniversity mod
    edited June 26

    @toyins said:
    Am I missing something?

    I haven't tried it (I'm in the midst of something else right now), but I suspect ScreenDensity is not returning the expected value.

    On Android, you would use the xdpi and ydpi values (for the pixels per inch in the formula I posted previously), as per this post: https://stackoverflow.com/questions/2193457/is-there-a-way-to-determine-android-physical-screen-height-in-cm-or-inches

    To use that, you'll probably want to use a DependencyService.

    I don't use Xamarin.Essentials currently, as every time I look at using it I find a limitation. One day perhaps.

  • toyinstoyins USMember ✭✭✭
    edited June 26

    @ColeX, I tried that but it did not work. I'm currently testing on a 5.7" Marshmallow Emulator.
    My question is, how do I know it's a 5.7" device?
    I tried the width and the height but without much luck.
    Here's the sample code.
    var w = metrics.WidthPixels;
    var x = metrics.Xdpi;
    var i = w / x;

    w=1440
    x=72
    The value for i is 20 in my situation.

  • JohnHardmanJohnHardman GBUniversity mod
    edited June 26

    @toyins

    This is Android code, rather than using Xamarin.Essentials:

    var windowService = GetSystemService(Context.WindowService);
    IWindowManager windowManager = windowService.JavaCast<IWindowManager>();
    
    DisplayMetrics metrics = new DisplayMetrics();
    windowManager.DefaultDisplay.GetMetrics(metrics);
    

    Using that, I am getting what look like correct results when using

    metrics.WidthPixels / metrics.Xdpi
    

    The same is true if I use the RealMetrics API rather than Metrics.

    Looking quickly at the Xamarin.Essentials code at https://github.com/xamarin/Essentials/blob/master/Xamarin.Essentials/DeviceDisplay/DeviceDisplay.android.cs
    it doesn't appear to take into account the Xdpi.

    You might want to consider using a DependencyService rather than Xamarin.Essentials, as per my post at https://forums.xamarin.com/discussion/comment/379910/#Comment_379910

  • toyinstoyins USMember ✭✭✭
    edited June 26

    @JohnHardman
    Using your example the answer I got was 20 instead of 5.7 for the 5.7" Marshmallow Emulator.
    Here's the sample code.

                var windowService = GetSystemService(Context.WindowService);
            IWindowManager windowManager = windowService.JavaCast<IWindowManager>();
    
            DisplayMetrics metrics = new DisplayMetrics();
            windowManager.DefaultDisplay.GetMetrics(metrics);
    
            var w = metrics.WidthPixels;
            var x = metrics.Xdpi;
    
            var i = w / x;
            Console.WriteLine(i.ToString(CultureInfo.InvariantCulture));
    

    Where w =1440 and x = 72.

  • JohnHardmanJohnHardman GBUniversity mod

    @toyins said:
    @JohnHardman
    Using your example the answer I got was 20 instead of 5.7 for the 5.7" Marshmallow Emulator.

    Try it on a physical device.

  • toyinstoyins USMember ✭✭✭

    @JohnHardman,
    I tried on a Samsung 8 Plus device which has a screen size of 6.2" and resolution 1440 x 2960.
    The width from debugging is 1080 and the XDPI is 391.8855, which results in having the Screen size to be
    2.755907 = 1080 / 391.8855
    which is not exactly right.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Would that not be the width of your screen?

  • JohnHardmanJohnHardman GBUniversity mod

    @toyins said:
    @JohnHardman,
    I tried on a Samsung 8 Plus device which has a screen size of 6.2" and resolution 1440 x 2960.
    The width from debugging is 1080 and the XDPI is 391.8855, which results in having the Screen size to be
    2.755907 = 1080 / 391.8855
    which is not exactly right.

    If you mean the Samsung Galaxy S8 Plus, I just Googled the size. The result that came up is 2.89 inches wide and 6.28 inches high. Assuming a couple of millimeters for the edge, I'd believe the screen width is 2.755907 inches.

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited June 26

    A screen size is expressed in diagonal, so a 5.7" screen has a diagonal of 5.7", same for any screen size (i.e. your 55" TV).

    Back to middle school math, a diagonal of a rectangle is, according to Mr. Pythagoras is:

    d = sqrt(width^2 + height^2).

    So the result is (not tested):

    // or use Android's DisplayMetrics to get sizes if Essentials provide wrong values
    var widthInInches = mainDisplayInfo.Width / mainDisplayInfo.Density; 
    var heightInInches = mainDisplayInfo.Height / mainDisplayInfo.Density;
    
    var screenSizeInInches = Math.Sqrt(Math.Pow(widthInInches, 2) + Math.Pow(heightInInches, 2));
    

    EDIT: Checked Essentials source code as pointed out by @JohnHardman, yes Density in Essentials returns the ratio of dip/pixel, not the screen density of pixels/inch (dpi). Don't know if it's a bug or it's intended to return this (nearly useless in Xamarin.Forms) metric. So you need to use Android's DisplayMetrics.

  • toyinstoyins USMember ✭✭✭
    edited June 27

    @Amar_Bait, using your formula, the screenSizeInInches was 897.564234007419 and I'm currently using the Samsung Galaxy S8 Plus for testing. The expected screen size for that device is 6.2".

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    What's the density you're getting from Xdpi?
  • toyinstoyins USMember ✭✭✭
    edited June 27

    @Amar_Bait, using the Xdpi and Ydpi, the answer is a lot closer than using the density.
    Here's the new formula.

                var widthInInches = metrics.WidthPixels / metrics.Xdpi;
                var heightInInches = metrics.HeightPixels / metrics.Ydpi;
    
                var screenSizeInInches = Math.Sqrt(Math.Pow(widthInInches, 2) + Math.Pow(heightInInches, 2));
    

    The Xdpi is 391.8855.

    The screenSizeInInches is now 5.98291193609726.

    The density was 2.625.

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited June 27

    @Amar_Bait said:
    EDIT: Checked Essentials source code as pointed out by JohnHardman, yes Density in Essentials returns the ratio of dip/pixel, not the screen density of pixels/inch (dpi). Don't know if it's a bug or it's intended to return this (nearly useless in Xamarin.Forms) metric. So you need to use Android's DisplayMetrics.

    Yes that what I told you in the edit, to use Xdpi/Ydpi. How much are you getting from Xdpi? for a Galaxy S8+ should be around 530 ppi. Screen resolution should be 1440 x 2960.

    So you would have:

    Screensize = Sqrt((1440/530)^2 + (2960/530)^2) = Sqrt(7.38 + 31.13) = 6.2

  • toyinstoyins USMember ✭✭✭
    edited June 27

    @Amar_Bait,
    Xdpi = 391.8855
    What I found baffling as well is that the WidthPixels is 1080 and the HeightPixels is 2094
    Not exactly 1440 x 2960.
    This is using the DisplayMetrics.
    Why is it not reporting the exact Width and Height?

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited June 27

    Try with DefaultDisplay.GetRealMetrics instead of DefaultDisplay.GetMetrics?

  • toyinstoyins USMember ✭✭✭
    edited June 27

    @Amar_Bait,
    Same result, code below.
    DisplayMetrics metrics = new DisplayMetrics();
    windowManager.DefaultDisplay.GetRealMetrics(metrics);

    var widthInInches = metrics.WidthPixels / metrics.Xdpi;
    var heightInInches = metrics.HeightPixels / metrics.Ydpi;
    var screenSizeInInches = Math.Sqrt(Math.Pow(widthInInches, 2) + Math.Pow(heightInInches, 2));

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭

    Shouldn't all sizes use metrics2?

  • toyinstoyins USMember ✭✭✭
    edited June 27

    Sorry that was just typo. I've corrected it above.

  • toyinstoyins USMember ✭✭✭
    Accepted Answer

    @Amar_Bait, you were right, my screen resolution was set to FHD+(2220x1080), changing to WQHD+ showed the resolution as 1440 x 2960.
    Thanks @Amar_Bait, @JohnHardman, @JoeManke and @ColeX.
    I really appreciate it.

    I used DependencyService within Xamrin.Forms to retrieve the value.

  • JohnHardmanJohnHardman GBUniversity mod

    @toyins said:
    I used DependencyService within Xamrin.Forms to retrieve the value.

    Did you end up using the code from https://forums.xamarin.com/discussion/comment/380025/#Comment_380025 ?

  • toyinstoyins USMember ✭✭✭

    I used @Amar_Bait's formula.

    var widthInInches = metrics.WidthPixels / metrics.Xdpi;
    var heightInInches = metrics.HeightPixels / metrics.Ydpi;
    var screenSizeInInches = Math.Sqrt(Math.Pow(widthInInches, 2) + Math.Pow(heightInInches, 2));

  • JohnHardmanJohnHardman GBUniversity mod
    edited June 28

    @toyins

    So, you weren't after the width (and height), which I had provided previously (see previous link)? You were after the diagonal (simple Pythagoras once you have the width and height)? Can I ask why? It's always seemed a particularly useless piece of information whether referring to TVs, PC monitors, mobile devices, or anything else. What will you use that information for? Genuinely curious.

  • toyinstoyins USMember ✭✭✭

    On Certain pages I needed to know the Screen Size of the Android Device.

  • JohnHardmanJohnHardman GBUniversity mod

    @toyins said:
    On Certain pages I needed to know the Screen Size of the Android Device.

    But why do those pages need it? What do you do with the diagonal size? Does your code do something different depending on what the size is?

  • toyinstoyins USMember ✭✭✭

    Yes.

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited June 28

    @toyins You should mark @JohnHardman and @ColeX answers as answers, since they were the 1st ones who showed you how to get Width/Height/DpiX from Android since Essentials Density doesn't mean what it looks like, what I did is just assist you to find why you weren't getting the width/height as stated in the phone specs.

Sign In or Register to comment.