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.

Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
Tariq Hussain
  • 409
  • 1
  • 5
  • 19
  • If you use it to get a screen size, please see my answer here https://stackoverflow.com/a/70087378/1731626. There is maintainable implementation of getting the screen size written in Kotlin. – Sergio Dec 15 '21 at 17:02

5 Answers5

3

try this

Resources.getSystem().displayMetrics

For width and height

Resources.getSystem().displayMetrics.widthPixels
Resources.getSystem().displayMetrics.heightPixels
Ehsan Ullah
  • 144
  • 3
3

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.

Sergio
  • 27,326
  • 8
  • 128
  • 149
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.

Per.J
  • 1,229
  • 8
  • 12
1

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()
Jatin
  • 1,650
  • 3
  • 17
  • 32
1

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;
    }