DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Kai
Kai

Posted on

Get Android's screen width and height

Inside your Activity:

override fun onStart() {
    val size = Point()
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
        requireContext().display!!.getRealSize(size)
    } else {
        val windowManager = requireContext().getSystemService(Context.WINDOW_SERVICE) as WindowManager
        windowManager.defaultDisplay.getRealSize(size)
    }
    size.x // Width
    size.y // Height
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

DEV

Thank you.

Β 
Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.