0
0
Android Kotlinmobile~20 mins

Android SDK and API levels in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: SDK Info Screen
This screen shows the current Android SDK version and API level of the device. It helps users understand what Android version their device is running.
Target UI
-------------------------
|    SDK Info Screen     |
|-----------------------|
| SDK Version:          |
| API Level:            |
|                       |
| [Refresh]             |
-------------------------
Display the current Android SDK version as a text label.
Display the current Android API level as a text label.
Add a Refresh button that updates the displayed SDK version and API level when tapped.
Use a vertical layout with proper spacing.
Ensure the text is centered horizontally.
Use accessible labels for screen readers.
Starter Code
Android Kotlin
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class SdkInfoActivity : AppCompatActivity() {
    private lateinit var sdkVersionTextView: TextView
    private lateinit var apiLevelTextView: TextView
    private lateinit var refreshButton: Button

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

        sdkVersionTextView = findViewById(R.id.sdkVersionTextView)
        apiLevelTextView = findViewById(R.id.apiLevelTextView)
        refreshButton = findViewById(R.id.refreshButton)

        // TODO: Add code to display SDK version and API level
        // TODO: Add click listener to refreshButton to update info
    }
}
Task 1
Task 2
Task 3
Solution
Android Kotlin
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class SdkInfoActivity : AppCompatActivity() {
    private lateinit var sdkVersionTextView: TextView
    private lateinit var apiLevelTextView: TextView
    private lateinit var refreshButton: Button

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

        sdkVersionTextView = findViewById(R.id.sdkVersionTextView)
        apiLevelTextView = findViewById(R.id.apiLevelTextView)
        refreshButton = findViewById(R.id.refreshButton)

        fun updateSdkInfo() {
            val sdkVersion = Build.VERSION.RELEASE ?: "Unknown"
            val apiLevel = Build.VERSION.SDK_INT.toString()
            sdkVersionTextView.text = "SDK Version: $sdkVersion"
            apiLevelTextView.text = "API Level: $apiLevel"
        }

        updateSdkInfo()

        refreshButton.setOnClickListener {
            updateSdkInfo()
        }
    }
}

This code uses Build.VERSION.RELEASE to get the Android SDK version as a string, like "13" or "12". It uses Build.VERSION.SDK_INT to get the API level as an integer, like 33 or 31.

We define a function updateSdkInfo() that sets the text of the two TextViews with this info. We call it once when the screen loads to show the info immediately.

The Refresh button has a click listener that calls updateSdkInfo() again, so if the device info changes (rare but possible), the user can update the display.

This keeps the UI simple and clear, with accessible text labels and a button that updates the info on demand.

Final Result
Completed Screen
-------------------------
|    SDK Info Screen     |
|-----------------------|
| SDK Version: 13       |
| API Level: 33         |
|                       |
| [Refresh]             |
-------------------------
When the screen opens, it shows the current SDK version and API level.
Tapping the Refresh button updates the SDK version and API level displayed.
Stretch Goal
Add a dark mode toggle button that switches the screen background and text colors between light and dark themes.
💡 Hint
Use AppCompatDelegate.setDefaultNightMode() with MODE_NIGHT_YES and MODE_NIGHT_NO to switch themes programmatically.