I used the getSize() method to get the screen sizes:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val fragmentActivity = requireActivity()
...
val wm = fragmentActivity.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
fragmentActivity.display
} else {
wm.defaultDisplay
}
val size = Point()
display?.getSize(size)
// get screen sizes
val width = size.x
val height = size.y
...
}
But with API level 30 the method getSize() is declared deprecated.
What can be used instead of getSize() for obtain screen sizes?
Thank you for any comment/answer!
Solution:
val wm = fragmentActivity.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val width: Int
val height: Int
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics = wm.currentWindowMetrics
val windowInsets: WindowInsets = windowMetrics.windowInsets
val insets = windowInsets.getInsetsIgnoringVisibility(
WindowInsets.Type.navigationBars() or WindowInsets.Type.displayCutout())
val insetsWidth = insets.right + insets.left
val insetsHeight = insets.top + insets.bottom
val b = windowMetrics.bounds
width = b.width() - insetsWidth
height = b.height() - insetsHeight
} else {
val size = Point()
val display = wm.defaultDisplay // deprecated in API 30
display?.getSize(size) // deprecated in API 30
width = size.x
height = size.y
}
"Use
WindowManager#getCurrentWindowMetrics()
to obtain an instance of WindowMetrics and use WindowMetrics#getBounds() instead."
It's from Android documentation. [Android doc] https://developer.android.com/reference/android/view/Display#getSize(android.graphics.Point)