display.getRealMetrics() is deprecated
Asked Answered
D

5

8

I was using getRealMetrics() method and came to know that it is deprecated

val display = this.display
display?.getRealMetrics(outMetrics)

anybody know what is the alternative.

Dobla answered 18/11, 2021 at 7:5 Comment(1)
If you use it to get a screen size, please see my answer here https://mcmap.net/q/36684/-get-screen-width-and-height-in-android. There is maintainable implementation of getting the screen size written in Kotlin.Clung
C
4

According to the official docs the recommended way is to use WindowManager#getCurrentWindowMetrics():

val metrics: WindowMetrics = context.getSystemService(WindowManager::class.java).currentWindowMetrics

If you use it to get screen size, please see my answer here.

Clung answered 23/11, 2021 at 20:34 Comment(0)
M
3

try this

Resources.getSystem().displayMetrics

For width and height

Resources.getSystem().displayMetrics.widthPixels
Resources.getSystem().displayMetrics.heightPixels
Metabolite answered 18/11, 2021 at 7:11 Comment(2)
How I can use it as a substitute of display.getRealMetrics(displaymetrics)Dobla
I got it thank youDobla
O
2

Use WindowMetricsCalculator to get display height and width parameter

dependencies {
implementation "androidx.window:window:1.0.0-beta02"}


val windowMetrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(activity)
val currentBounds = windowMetrics.bounds

val width = currentBounds.width()
val height = currentBounds.height()
Organization answered 19/11, 2021 at 5:17 Comment(0)
F
2

Hi i create this function in xamarin.android: (manifest SDK max 31, min 25)

public bool IsTablet()
    {
        var displayMetrics = Resources.DisplayMetrics;
        var defaultDisplay = WindowManager.DefaultDisplay;
        double widthPixels = 0;
        double heightPixels = 0;

        if (Build.VERSION.SdkInt >= BuildVersionCodes.S) //API 31 and more
        {
            var windowMetrics = WindowManager.CurrentWindowMetrics;

            Rect bounds = windowMetrics.Bounds;
            widthPixels = bounds.Width();
            heightPixels = bounds.Height();   
        }
        if (Build.VERSION.SdkInt == BuildVersionCodes.R) //API 30
        {
            #pragma warning disable
            defaultDisplay.GetRealMetrics(displayMetrics);

            widthPixels = displayMetrics.WidthPixels;
            heightPixels = displayMetrics.HeightPixels;
        }
        if (Build.VERSION.SdkInt < BuildVersionCodes.R) //less then 30
        {
            #pragma warning disable
            defaultDisplay.GetMetrics(displayMetrics);

            widthPixels = displayMetrics.WidthPixels;
            heightPixels = displayMetrics.HeightPixels;
        }

        var wInches1 = widthPixels / (double)displayMetrics.DensityDpi;
        var hInches1 = heightPixels / (double)displayMetrics.DensityDpi;

        double inch = Math.Sqrt(Math.Pow(wInches1, 2) + Math.Pow(hInches1, 2));

        if (cale >= 7.0)
        {
            tablet = true;
        }

        return tablet;
    }
Friedrich answered 22/3, 2022 at 17:50 Comment(0)
P
1

https://developer.android.com/reference/android/view/Display

getRealMetrics(DisplayMetrics outMetrics)

This method was deprecated in API level 31. Use WindowManager#getCurrentWindowMetrics() to identify the current size of the activity window. UI-related work, such as choosing UI layouts, should rely upon WindowMetrics#getBounds(). Use Configuration#densityDpi to get the current density.

Polly answered 18/11, 2021 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.