Closed FANMixco closed 2 years ago
Yes, this is expected. Using older deprecated APIs will always produce a warning. If you're guarding it with runtime API check like you did in your code then it's fine. On newer devices with API 30+ the 2nd part of the if statement will run, on older APIs, the new methods and classes aren't even available to be called so you will have to use the deprecated methods.
Just suppress the warning in-source for that particular call.
Hi @AmrAlSayed0, I know that. My question is: "What is the current code for the SDK 30+? Or why am I getting the warning if my code is correct?" My solution is based on ones that you can find in StackOverflow that in theory are updated for the SDK 30+, for example:
val outMetrics = DisplayMetrics()
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
val display = activity.display
display?.getRealMetrics(outMetrics)
} else {
@Suppress("DEPRECATION")
val display = activity.windowManager.defaultDisplay
@Suppress("DEPRECATION")
display.getMetrics(outMetrics)
}
Any suggestion?
Hi @AmrAlSayed0, I know that. My question is: "What is the current code for the SDK 30+? Or why am I getting the warning if my code is correct?" My solution is based on ones that you can find in StackOverflow that in theory are updated for the SDK 30+, for example:
val outMetrics = DisplayMetrics()
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) { val display = activity.display display?.getRealMetrics(outMetrics) } else { @Suppress("DEPRECATION") val display = activity.windowManager.defaultDisplay @Suppress("DEPRECATION") display.getMetrics(outMetrics) }
Any suggestion?
Currently Device Display Information available via official Xamarin.Essentials nuget package, see: https://docs.microsoft.com/en-us/xamarin/essentials/device-display
i hope this could help you
After a while, I created this code based on multiple answers:
private double GetScreenSize()
{
float yInches;
float xInches;
if (Build.VERSION.SdkInt >= BuildVersionCodes.R)
{
var windowMetrics = WindowMetricsCalculator.Companion.OrCreate.ComputeCurrentWindowMetrics(Activity);
var currentBounds = windowMetrics.Bounds;
var metrics2 = RequireActivity().WindowManager.CurrentWindowMetrics;
var insets = metrics2.WindowInsets.GetInsets(WindowInsets.Type.SystemBars());
yInches = (metrics2.Bounds.Height() - insets.Bottom - insets.Top) / Context.Resources.DisplayMetrics.Ydpi;
xInches = (metrics2.Bounds.Width() - insets.Left - insets.Right) / Context.Resources.DisplayMetrics.Xdpi;
}
else
{
using DisplayMetrics metrics = new DisplayMetrics();
#pragma warning disable CS0618 // Type or member is obsolete
Activity.WindowManager.DefaultDisplay.GetMetrics(metrics);
#pragma warning restore CS0618 // Type or member is obsolete
yInches = metrics.HeightPixels / metrics.Ydpi;
xInches = metrics.WidthPixels / metrics.Xdpi;
}
return Math.Round(Math.Sqrt(xInches * xInches + yInches * yInches), 0);
}
You require this library:
I'm trying to upgrade a section of my code and I'm targeting Android 30. This is my original code:
Then, I'm trying to upgrade the code to this one:
Regardless of my actions, my code is still considered as Obsolete:
Now, Google suggests you use:
Source: https://developer.android.com/reference/android/view/WindowManager
But I cannot find it anywhere:
Any idea what am I doing wrong? Or if any binding is missing? Thanks for your support.
VS Version: