Trying to port some old code and not sure how to translate a screen size measurement intended to be in defined in inches to the units MAUI is using. It seems like DisplayInfo
's Width
and Height
properties and Density
are not sufficient for me to calculate what the actual width/height units come out as on a full-screen page and I'm not sure exactly how the units are calculated?
It seems MAUI may be sparse on documentation related to this, are the Xamarin docs still relevant (as this older answer proposes)? It's hard to know how much has changed and I might be mathing wrong somewhere, but if I take the documentation that links to along with my not-quite-device-accurate "Pixel 5" emulator with a resolution of 1080x2340 and a DPI of 440 the numbers don't work out quite right. I tinkered given the following measurements:
DeviceDisplay.Current.MainDisplayInfo.Height: 2340
DeviceDisplay.Current.MainDisplayInfo.Width: 1080
DeviceDisplay.Current.MainDisplayInfo.Density: 2.75
2.75 * 160 = 440dpi, so the density seems to match the docs
2340 / 440 = 5.31818..." tall
1080 / 440 = 2.4545..." wide
5.31818..." * 160dps = 850.9090...dps, should be the full height of the display
2.4545..." * 160dps = 392.7272...dps, should be the full width of the display
but my observations didn't match my expectations, so I added an override void OnSizeAllocated
into a launch page that is almost full height (expecting something just under the above 850 leaving some room for the notification/action bars) and wrote out the actual width/height which showed:
OnSizeAllocated, width: 320, height: 654.1818181818181
So based on these measurements, it seems like the screen is only 2 inches wide and the height is perceived as 4 inches tall. I have no idea how to predict/calculate these seemingly arbitrary differences from the (Xamarin) docs. Has MAUI changed the behavior? Is there a factor I'm not taking into consideration?
Let's say I wanted to specify something should have a max-width of 2.5 inches or change the height of something if the screen is taller than 5 inches, how would I express that (preferably in C#, as this app is not using XAML)? If it makes a difference, I only need this to work for iOS and Android. Alternatively, if there's no guidance for what I'm looking for, is there a nice easy cross-platform way to ask what the device's physical screen dimensions are that I might use to better boil down these ratios?
After a bit of digging I found the cause of the inconsistency was actually in the AndroidManifest.xml file. I had to view the raw xml rather than the editor and found this suspicious looking line which ended up being the reason the width was fixed:
<supports-screens android:largestWidthLimitDp="320" />