0
0
Android Kotlinmobile~15 mins

Data classes in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: User Profile Screen
This screen shows a simple user profile using a Kotlin data class to hold user information.
Target UI
-------------------------
| User Profile          |
|-----------------------|
| Name:                 |
| Email:                |
| Age:                  |
|                       |
| [Show User Info]      |
-------------------------
Create a Kotlin data class named User with properties: name (String), email (String), and age (Int).
Add a button labeled 'Show User Info'.
When the button is clicked, display the user's name, email, and age below the button.
Starter Code
Android Kotlin
package com.example.dataclassdemo

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)

        val showInfoButton: Button = findViewById(R.id.showInfoButton)
        val userInfoTextView: TextView = findViewById(R.id.userInfoTextView)

        // TODO: Create User data class instance here

        showInfoButton.setOnClickListener {
            // TODO: Display user info in userInfoTextView
        }
    }
}
Task 1
Task 2
Task 3
Solution
Android Kotlin
package com.example.dataclassdemo

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

data class User(val name: String, val email: String, val age: Int)

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

        val showInfoButton: Button = findViewById(R.id.showInfoButton)
        val userInfoTextView: TextView = findViewById(R.id.userInfoTextView)

        val user = User("Alice Johnson", "alice@example.com", 28)

        showInfoButton.setOnClickListener {
            userInfoTextView.text = "Name: ${user.name}\nEmail: ${user.email}\nAge: ${user.age}"
        }
    }
}

We defined a Kotlin data class named User with three properties: name, email, and age. This class automatically provides useful functions like toString() and equals().

In the MainActivity, we created an instance of User with sample data.

When the user taps the 'Show User Info' button, the app updates the TextView to display the user's details. This shows how data classes help organize and hold data cleanly in Kotlin apps.

Final Result
Completed Screen
-------------------------
| User Profile          |
|-----------------------|
|                       |
|                       |
|                       |
| [Show User Info]      |
|                       |
| Name: Alice Johnson   |
| Email: alice@example.com |
| Age: 28               |
-------------------------
User taps the 'Show User Info' button.
The user's name, email, and age appear below the button.
Stretch Goal
Add a Clear button that clears the displayed user info when tapped.
💡 Hint
Add another Button with an onClickListener that sets the TextView text to an empty string.