0
0
Android Kotlinmobile~20 mins

Data types and type inference in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Data Types Demo
This screen shows examples of Kotlin data types and how Kotlin infers types automatically.
Target UI
-------------------------
| Data Types Demo       |
|-----------------------|
| Int value:            |
| String value:         |
| Boolean value:        |
| Double value:         |
| Inferred Int:         |
| Inferred String:      |
| Inferred Boolean:     |
| Inferred Double:      |
|                       |
| [Show Values]         |
-------------------------
Display labels for Int, String, Boolean, and Double variables with explicit types.
Display labels for variables with inferred types (no explicit type declared).
Add a button labeled 'Show Values' that when tapped, shows the values of all variables below their labels.
Use Kotlin's type inference for the inferred variables.
Use a simple vertical layout with clear spacing.
Starter Code
Android Kotlin
package com.example.datatypesdemo

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

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

        // TODO: Define variables with explicit types
        // TODO: Define variables with inferred types

        val showButton: Button = findViewById(R.id.showButton)

        showButton.setOnClickListener {
            // TODO: Show variable values in TextViews
        }
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
package com.example.datatypesdemo

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

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

        // Explicitly typed variables
        val explicitInt: Int = 42
        val explicitString: String = "Hello Kotlin"
        val explicitBoolean: Boolean = true
        val explicitDouble: Double = 3.14

        // Inferred type variables
        val inferredInt = 100
        val inferredString = "Type Inference"
        val inferredBoolean = false
        val inferredDouble = 2.718

        val showButton: Button = findViewById(R.id.showButton)

        val explicitIntValue: TextView = findViewById(R.id.explicitIntValue)
        val explicitStringValue: TextView = findViewById(R.id.explicitStringValue)
        val explicitBooleanValue: TextView = findViewById(R.id.explicitBooleanValue)
        val explicitDoubleValue: TextView = findViewById(R.id.explicitDoubleValue)

        val inferredIntValue: TextView = findViewById(R.id.inferredIntValue)
        val inferredStringValue: TextView = findViewById(R.id.inferredStringValue)
        val inferredBooleanValue: TextView = findViewById(R.id.inferredBooleanValue)
        val inferredDoubleValue: TextView = findViewById(R.id.inferredDoubleValue)

        showButton.setOnClickListener {
            explicitIntValue.text = explicitInt.toString()
            explicitStringValue.text = explicitString
            explicitBooleanValue.text = explicitBoolean.toString()
            explicitDoubleValue.text = explicitDouble.toString()

            inferredIntValue.text = inferredInt.toString()
            inferredStringValue.text = inferredString
            inferredBooleanValue.text = inferredBoolean.toString()
            inferredDoubleValue.text = inferredDouble.toString()
        }
    }
}

This app screen demonstrates Kotlin data types and type inference.

We declare variables with explicit types like Int, String, Boolean, and Double. Then we declare variables without specifying types, letting Kotlin infer them automatically.

When the user taps the 'Show Values' button, the app displays the values of all variables below their labels. This shows how Kotlin handles both explicit and inferred types clearly.

Final Result
Completed Screen
-------------------------
| Data Types Demo       |
|-----------------------|
| Int value: 42         |
| String value: Hello Kotlin |
| Boolean value: true   |
| Double value: 3.14    |
| Inferred Int: 100     |
| Inferred String: Type Inference |
| Inferred Boolean: false|
| Inferred Double: 2.718|
|                       |
| [Show Values]         |
-------------------------
User taps 'Show Values' button.
Values of all variables appear next to their labels.
Screen updates instantly showing explicit and inferred values.
Stretch Goal
Add a toggle switch to switch between light and dark theme for the screen.
💡 Hint
Use AppCompatDelegate.setDefaultNightMode() with MODE_NIGHT_YES and MODE_NIGHT_NO to switch themes.