How do you detect if a device is small or big? I dont need to detect if is a tablet or not.
This concept was taken from https://devblogs.microsoft.com/xamarin/styling-for-multiple-device-resolutions/
We use the method below and load the appropriate styles accordingly, however because I dont take into account density it is not accurate.
How can I improve or rewrite this method so that it gives me a better result and detects more accurately if a device is big or small.
Current
const int smallWightResolution = 768;
const int smallHeightResolution = 1280;
public static bool IsASmallDevice()
{
// Get Metrics
var mainDisplayInfo = Xamarin.Essentials.DeviceDisplay.MainDisplayInfo;
// Width (in pixels)
var width = mainDisplayInfo.Width;
// Height (in pixels)
var height = mainDisplayInfo.Height;
return (width <= smallWightResolution && height <= smallHeightResolution);
}
Attempt to use density but don't know what the formula is
public static bool IsSmallDevice()
{
//we don't support tablet so tablet don't apply.
int smallWidthResolution = 768;
int smallHeightResolution = 1280;
double screenWidth;
double screenHeight;
bool isSmallDevice;
var metrics = Xamarin.Essentials.DeviceDisplay.MainDisplayInfo;
switch (Xamarin.Forms.Device.RuntimePlatform)
{
case Xamarin.Forms.Device.Android:
//Android not sure how to sort of correctly detect if is a small device
screenWidth = (metrics.Width - 0.5f) / metrics.Density;
screenHeight = (metrics.Height - 0.5f) / metrics.Density;
isSmallDevice = "???????";
break;
case Xamarin.Forms.Device.iOS:
//ios no changes
isSmallDevice = metrics.Width <= smallWidthResolution
&& metrics.Height <= smallHeightResolution;
break;
}
return isSmallDevice;
}
UPDATED
Huawei P9 Android 7.0
Density=2.5
Width=1080
Height=2160
ScreenHeight(Calculated)=864
ScreenWidth(Calculated)=432
Samsung A20 Android (new phone)
Density=2
Width=720
Height=1560
ScreenHeight(Calculated)=780
ScreenWidth(Calculated)=360
Any suggestions?