Search code examples
android-studioandroid-windowmanager

What is equivalent to defaultDisplay.getRealSize(point) in api 30?


In api level 30 defaultDisplay is deprecated. So we need to use currentWindowMetrics. But there is no any getRealSize method in it. What will be equivalent to code:

windowManager.defaultDisplay.getRealSize(screenRealSize)

in API level 30?


Solution

  • It is getBounds in windowMetrics, so we need to this method and set width and height separately to our variable like this:

        val screenRealSize = Point()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val windowMetrics = activity.windowManager.currentWindowMetrics
            screenRealSize.x = windowMetrics.bounds.width()
            screenRealSize.y = windowMetrics.bounds.height()
        } else {
            activity.windowManager.defaultDisplay.getRealSize(screenRealSize)
        }