Skip to content

Instantly share code, notes, and snippets.

@jonrysimbolon
Last active April 19, 2024 12:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonrysimbolon/c7ad7144b17e71c3261e13c008e00e0f to your computer and use it in GitHub Desktop.
Save jonrysimbolon/c7ad7144b17e71c3261e13c008e00e0f to your computer and use it in GitHub Desktop.
Deprecated "windowManager.defaultDisplay.getMetrics(displayMetrics)"
private fun screenValue() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val defaultDisplay =
DisplayManagerCompat.getInstance(this).getDisplay(Display.DEFAULT_DISPLAY)
val displayContext = createDisplayContext(defaultDisplay!!)
val width = displayContext.resources.displayMetrics.widthPixels
val height = displayContext.resources.displayMetrics.heightPixels
Log.e(tag, "width (ANDOIRD R/ABOVE): $width")
Log.e(tag, "height (ANDOIRD R/ABOVE) : $height")
} else {
val displayMetrics = DisplayMetrics()
@Suppress("DEPRECATION")
windowManager.defaultDisplay.getMetrics(displayMetrics)
val height = displayMetrics.heightPixels
val width = displayMetrics.widthPixels
Log.e(tag, "width (BOTTOM ANDROID R): $width")
Log.e(tag, "height (BOTTOM ANDROID R) : $height")
}
}
@intersk
Copy link

intersk commented Oct 25, 2022

"width = displayContext.resources.displayMetrics.widthPixels"
why not "val width = .... " ?

@jonrysimbolon
Copy link
Author

@intersk thanks, i forgot it

@yusufkhan08070817
Copy link

how i can get updated function or method of any deprecated function or method where can i read

@Gredys
Copy link

Gredys commented Oct 3, 2023

it still returns the size EXCLUDING the fucking systems bars, that's super annoying >..<

@Dubemtopsite
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment