Get Android and SDK version using Kotlin

 Exercise to get Android device info and compile SDK version using Kotlin.

Run on Android Emulator of Android 10

Run on Redmi 5 Plus (Android 8.1.0)

MainActivity.kt

package com.blogspot.android_er.androidinfo

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.annotation.RequiresApi

class MainActivity : AppCompatActivity() {

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

val manufacturer = Build.MANUFACTURER
val model = Build.MODEL
val release = Build.VERSION.RELEASE

val pkgInfo = packageManager.getPackageInfo(packageName, 0)
val appInfo = pkgInfo.applicationInfo
val targetSdkVersion = appInfo.targetSdkVersion
val minSdkVersion = appInfo.minSdkVersion

//Display system and SDK info for reference
val tvInfo = findViewById<TextView>(R.id.info)
tvInfo.setText(manufacturer + "\n" +
model + "\n" +
"Android: " + release + "\n" +
"targetSdkVersion: " + targetSdkVersion + "\n" +
"minSdkVersion: " + minSdkVersion)
}
}

layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android-er"
android:textStyle="bold"
android:textSize="34dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-er.blogspot.com " />
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textSize="30dp"/>

</LinearLayout>


Post a Comment

Previous Post Next Post