Search code examples
androidandroid-jetpack-compose

Screen width and height in Jetpack Compose


I am wondering how to get the screen width and height with Jetpack Compose?

Is there any way you can retrieve the screen dimensions other than getWindowManager().getDefaultDisplay()?


Solution

  • You can achieve this with LocalConfiguration.current:

    @Composable
    fun PostView() {
      val configuration = LocalConfiguration.current
    
      val screenHeight = configuration.screenHeightDp.dp
      val screenWidth = configuration.screenWidthDp.dp
    
      ...
    
    }