Friday, August 14, 2020

android kotlin - Get API level programmatically

MainActivity.kt

package com.example.jetpack

import android.os.Build
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)

        textView.text = "Device OS Details"

        // base OS build the product is based on
        textView.append("\n\nBase OS: ${Build.VERSION.BASE_OS}")

        // current development codename, or the string "REL"
        // if this is a release build
        textView.append("\nCodeName: ${Build.VERSION.CODENAME}")

        // internal value used by the underlying source
        // control to represent this build
        textView.append("\nIncremental: ${Build.VERSION.INCREMENTAL}")

        // developer preview revision of a pre-release sdk
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            textView.append("\nPreview SDK Int: ${Build.VERSION.PREVIEW_SDK_INT}")
        }

        // user-visible version string.
        textView.append("\nRelease: ${Build.VERSION.RELEASE}")

        // version string show to the user; may be RELEASE or
        // CODENAME if not a final release build
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            textView.append("\nRelease Or CodeName: " +
                    "${Build.VERSION.RELEASE_OR_CODENAME}")
        }

        // user-visible security patch level
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            textView.append("\nSecurity Patch: ${Build.VERSION.SECURITY_PATCH}")
        }

        // sdk version of the software currently running on this hardware device
        textView.append("\n\nSDK Int (API Level): ${Build.VERSION.SDK_INT}")
    }
}
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:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="16dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#191970"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>