android kotlin - Get screen size from context

MainActivity.kt

package com.example.jetpack

import android.content.Context
import android.content.Context.WINDOW_SERVICE
import android.graphics.Rect
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        textView.text = "API Level ${Build.VERSION.SDK_INT}"

        applicationContext.displayMetrics()?.apply {
            textView.append("\n\nUsing Display Metrics")
            textView.append("\n$widthPixels px * ")
            textView.append(" $heightPixels px")
        }

        applicationContext.bounds()?.apply {
            textView.append("\n\nUsing Bounds")
            textView.append("\n${width()} px * ${height()} px")
        }
    }
}


// extension function to get display metrics instance
fun Context.displayMetrics():DisplayMetrics?{
    // display metrics is a structure describing general information
    // about a display, such as its size, density, and font scaling
    val displayMetrics = DisplayMetrics()

    return if (Build.VERSION.SDK_INT <= 29){
        // get window manager system service from context
        val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager

        // getMetrics() method was deprecated in api level 30
        windowManager.defaultDisplay.getMetrics(displayMetrics)

        return displayMetrics
    }else{
        null
    }
}


// extension function to get window bounds
fun Context.bounds():Rect?{
    return if (Build.VERSION.SDK_INT >=30){
        // get window manager system service from context
        val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager

        // returns the bounds of the area associated with
        // this window or visual context
        windowManager.currentWindowMetrics.bounds
    }else{
        null
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#FFFAF0">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="16dp"
        android:fontFamily="sans-serif-condensed-medium"
        android:textColor="#D71868"
        tools:text="TextView"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials