How do i get running android device SDK version name and API version

Published October 07, 2022

While we run application on devices, how do we compare application version will support running device os version or not. To compare app version to device version we should know what is the current device version on programmatically. To get current device version we will use android Build.VERSION class which contains properties like SDK_INT, RELEASE name...

In this below example code we will learn how to get device SDK Version and API details

Let get started

1. Create android application in android studio

2. In this example we have two widgets Button and TextView, On tap on button we will write code for get SDK version details and add them to TextView.

Let update xml file with below code

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SDkVersion">

          <TextView
              android:id="@+id/txt_info"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_marginBottom="50dp"
              android:text=""
              android:textAlignment="center"
              android:textColor="@android:color/holo_green_dark"
              android:textSize="32sp"
              android:textStyle="bold"
              app:layout_constraintBottom_toTopOf="@+id/button"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent" />



          <Button
              android:id="@+id/button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Get Info"
              android:onClick="getInfo"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

3. Write code to get device sdk and API details on button click

Update activity with below code

package rrtutors.com.androidtopactivity

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView

class SDkVersion : AppCompatActivity() {
    lateinit var txt_info:TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sdk_version)
        txt_info=findViewById(R.id.txt_info)
    }

    fun getInfo(view: View) {
        val versionAPI = Build.VERSION.SDK_INT
        val versionRelease = Build.VERSION.RELEASE
        txt_info.text = "API Version : $versionAPI\nVersion Release : $versionRelease"

    }
}

 

4. Run application on your device/emulator. On Button click you will get the current device api version

Get Android SDK version name and API details programatically

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

92 Views