0
0
Android Kotlinmobile~20 mins

Variables (val, var) and null safety in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Variable Demo Screen
This screen shows how to declare variables using val and var, and how to handle null safety in Kotlin. It displays variable values and updates them on button clicks.
Target UI
-------------------------
| Variable Demo Screen   |
|-----------------------|
| val name: John        |
| var age: 25           |
| nullable city: null   |
|                       |
| [Change Age] [Set City]|
-------------------------
Display a val variable 'name' initialized to "John"
Display a var variable 'age' initialized to 25
Display a nullable String variable 'city' initialized to null
Add a button 'Change Age' that increases age by 1 when clicked
Add a button 'Set City' that sets city to "New York" when clicked
Show updated values on the screen after button clicks
Use Kotlin null safety features to safely display city or show "Unknown" if null
Starter Code
Android Kotlin
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class VariableDemoActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_variable_demo)

        // TODO: Declare variables here

        // TODO: Initialize UI elements

        // TODO: Set initial text values

        // TODO: Set button click listeners
    }
}
Task 1
Task 2
Task 3
Task 4
Task 5
Task 6
Task 7
Solution
Android Kotlin
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class VariableDemoActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_variable_demo)

        val name: String = "John"
        var age: Int = 25
        var city: String? = null

        val nameTextView = findViewById<TextView>(R.id.nameTextView)
        val ageTextView = findViewById<TextView>(R.id.ageTextView)
        val cityTextView = findViewById<TextView>(R.id.cityTextView)
        val changeAgeButton = findViewById<Button>(R.id.changeAgeButton)
        val setCityButton = findViewById<Button>(R.id.setCityButton)

        nameTextView.text = "val name: $name"
        ageTextView.text = "var age: $age"
        cityTextView.text = "nullable city: ${city ?: "Unknown"}"

        changeAgeButton.setOnClickListener {
            age += 1
            ageTextView.text = "var age: $age"
        }

        setCityButton.setOnClickListener {
            city = "New York"
            cityTextView.text = "nullable city: ${city ?: "Unknown"}"
        }
    }
}

We declare name as val because it does not change. age is var because it changes when the button is clicked. city is a nullable String? initialized to null.

We use the safe call with the Elvis operator ?: to show "Unknown" if city is null.

Buttons update the variables and the UI text accordingly, demonstrating variable mutability and null safety in Kotlin.

Final Result
Completed Screen
-------------------------
| Variable Demo Screen   |
|-----------------------|
| val name: John        |
| var age: 26           |
| nullable city: New York|
|                       |
| [Change Age] [Set City]|
-------------------------
Tapping 'Change Age' increases the age number by 1 and updates the display.
Tapping 'Set City' sets the city to 'New York' and updates the display from 'Unknown' to 'New York'.
Stretch Goal
Add a Reset button that sets age back to 25 and city back to null, updating the UI accordingly.
💡 Hint
Declare a button in the layout and set its click listener to reset variables and update TextViews.