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.