How does one Programmatically determine the Screen Inch size of a device i.e. 5", 5.7", etc.?
If you are test on Android platform , use DisplayMetrics Class.
It provides more detailed value than Xamarin.Essentials
, like
Xdpi :The exact physical pixels per inch of the screen in the X dimension.
Ydpi:The exact physical pixels per inch of the screen in the Y dimension.
Density:The logical density of the display.
HeightPixels:The absolute height of the display in pixels.
WidthPixels:The absolute width of the display in pixels.
var metrics = new DisplayMetrics(); WindowManager.DefaultDisplay.GetMetrics(metrics);
This is the test on my side .
{DisplayMetrics{density=1.5, width=480, height=800, scaledDensity=1.5, xdpi=240.0, ydpi=240.0}}
width in inches = (WidthPixels / Xdpi)
height in inches = (HeightPixels / Ydpi)
Since Galaxy S8+ supports multiple resolutions, maybe you're running in HD+ resolution, explaining the smaller pixel dimensions than the phone specs. Try setting the phone resolution to WQHD+ maybe you will get accurate dimensions. 5.99 is pretty close to 6.2, I think that's the best you can get.
@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
https://docs.microsoft.com/en-us/xamarin/essentials/device-display?context=xamarin/xamarin-forms&tabs=android
@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;
Screen density means pixels per square inch, and you have the number of pixels. Do the math.
@JoeManke, formula please, if you can?
@toyins
@JoeManke provided enough information for you to work this out.
Start with:
width in inches = (width in pixels / pixels per inch)
where you calculate pixels per inch from screen density as per @JoeManke 's post
@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.
Width = 1440
Density = 3.21875
i = 447.378640776699 which is far from 5.7".
Am I missing something?
If you are test on Android platform , use DisplayMetrics Class.
It provides more detailed value than
Xamarin.Essentials
, likeXdpi :The exact physical pixels per inch of the screen in the X dimension.
Ydpi:The exact physical pixels per inch of the screen in the Y dimension.
Density:The logical density of the display.
HeightPixels:The absolute height of the display in pixels.
WidthPixels:The absolute width of the display in pixels.
Usage
This is the test on my side .
formula
width in inches = (WidthPixels / Xdpi)
height in inches = (HeightPixels / Ydpi)
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.
@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.
@toyins
This is Android code, rather than using Xamarin.Essentials:
Using that, I am getting what look like correct results when using
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
@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.
Where w =1440 and x = 72.
Try it on a physical device.
@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.
Would that not be the width of your screen?
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.
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):
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.
@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_Bait, using the Xdpi and Ydpi, the answer is a lot closer than using the density.
Here's the new formula.
The Xdpi is 391.8855.
The screenSizeInInches is now 5.98291193609726.
The density was 2.625.
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
@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?
Try with DefaultDisplay.GetRealMetrics instead of DefaultDisplay.GetMetrics?
@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));
Shouldn't all sizes use metrics2?
Sorry that was just typo. I've corrected it above.
Since Galaxy S8+ supports multiple resolutions, maybe you're running in HD+ resolution, explaining the smaller pixel dimensions than the phone specs. Try setting the phone resolution to WQHD+ maybe you will get accurate dimensions. 5.99 is pretty close to 6.2, I think that's the best you can get.
@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.
Did you end up using the code from https://forums.xamarin.com/discussion/comment/380025/#Comment_380025 ?
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));
@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.
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?
Yes.
@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.