Android Kotlin: How to get screen size category

The code demonstrates how to get the device screen size category in an Android application. It achieves this by using the screenSizeCategory extension property defined in the Context class.

Here's a breakdown of the code:

  1. Imports: The code starts by importing necessary classes like Context, Configuration, Bundle, AppCompatActivity, and kotlinx.android.synthetic.main. The kotlinx.android.synthetic.main import allows for using synthetic imports to access views from the layout file in the activity class.

  2. MainActivity Class: The code defines an AppCompatActivity subclass named MainActivity. This class represents the main activity of the application.

  3. onCreate Method: The onCreate method is a lifecycle method that is called when the activity is first created. It's responsible for initializing the activity and setting up its UI.

    • In the onCreate method, the following steps are performed:
      • The super.onCreate(savedInstanceState) call is made to initialize the parent activity class.
      • The setContentView(R.layout.activity_main) method sets the layout file for the activity.
      • The text of the TextView with the ID textView is set to the screen size category using the applicationContext.screenSizeCategory property.
  4. screenSizeCategory Extension Property: The screenSizeCategory extension property is defined on the Context class. It retrieves the device screen size category using the resources.configuration.screenLayout property and a bitmask Configuration.SCREENLAYOUT_SIZE_MASK to isolate the screen size bits.

    • The property uses a when expression to map the screen size codes (e.g., Configuration.SCREENLAYOUT_SIZE_XLARGE) to human-readable categories (e.g., "XLarge screen").
  5. activity_main.xml: The activity_main.xml file defines the layout for the activity. It includes a single TextView element with the ID textView.

Overall, this code snippet provides a simple way to determine the screen size category of the Android device running the application. This information can be useful for adapting the application's UI or behavior based on the screen size.

In summary, the code defines an extension property on the Context class to get the screen size category of the device. It then uses this property to display the screen size category on a TextView in the activity's layout.


MainActivity.kt

package com.example.jetpack

import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
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)

        // show the device screen layout size category in text view
        textView.text = applicationContext.screenSizeCategory
    }
}


// extension property to get device screen layout size category
val Context.screenSizeCategory:String
    get() {
        return when(resources.configuration.screenLayout
                and Configuration.SCREENLAYOUT_SIZE_MASK) {
            Configuration.SCREENLAYOUT_SIZE_XLARGE -> "XLarge screen"
            Configuration.SCREENLAYOUT_SIZE_LARGE -> "Large screen"
            Configuration.SCREENLAYOUT_SIZE_NORMAL -> "Normal size screen"
            Configuration.SCREENLAYOUT_SIZE_SMALL -> "Small size screen"
            Configuration.SCREENLAYOUT_SIZE_UNDEFINED -> "Undefined screen size"
            else -> "Error"
        }
    }
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"
    android:background="#E5E4E2"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#191970"
        android:textSize="33sp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="TextView" />

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