android kotlin - Data class example

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.os.Bundle
import android.text.method.ScrollingMovementMethod
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)

        // get the widgets reference from XML layout
        val textView = findViewById<TextView>(R.id.textView)
        val btnDataClass = findViewById<Button>(R.id.btnDataClass)
        val btnDataClassDefault = findViewById<Button>(
            R.id.btnDataClassDefault)
        val btnDataClassCopy = findViewById<Button>(R.id.btnDataClassCopy)


        // make textview content scrollable
        textView.movementMethod = ScrollingMovementMethod()


        // Data class example
        btnDataClass.setOnClickListener{
            // Initialize a new user's instance
            val firstUser = User(1,"Jenny","Jones")

            // Display the first user's data in the text view
            textView.text = "First user is\nID : ${firstUser.id} \nName " +
                    ": ${firstUser.firstName} ${firstUser.lastName}"

            // Here we update the first user's name
            firstUser.firstName = "Kristina"

            // Again we display the first user's in text view
            textView.text = textView.text.toString() +
                    "\n\nAfter update, now first user is\n"
            textView.text = textView.text.toString() +
                    "ID : ${firstUser.id} \nName : ${firstUser.firstName}" +
                    " ${firstUser.lastName}"
        }


        // Data class with properties default values example
        btnDataClassDefault.setOnClickListener{
            // Initialize a customer instance
            val firstCustomer = Customer(1,"Jack")

            // Display it in text view
            textView.text = "First customer is\n"
            textView.text = textView.text.toString() +
                    "ID : ${firstCustomer.id}, Name: ${firstCustomer.name}," +
                    " City: ${firstCustomer.city}, Age : ${firstCustomer.age}"

            // Using property name
            val secondCustomer = Customer(id =2, age=30, name="Raymond")

            // Display the second customer data
            textView.text = textView.text.toString() +
                    "\n\nSecond customer is\n" +
                    "ID: ${secondCustomer.id}," +
                    " Name: ${secondCustomer.name}, " +
                    "City: ${secondCustomer.city}," +
                    " Age : ${secondCustomer.age}"
        }


        // Data class copy example
        btnDataClassCopy.setOnClickListener{
            // Initialize new user
            val user1 = User(1,"Alice","Jones")

            // Display the user1
            textView.text = "User1 is\n" +
                    "ID: ${user1.id}, Name :" +
                    " ${user1.firstName} ${user1.lastName} \n\n"

            // Copy the user1 and display it in text view
            val user1Copy = user1.copy(id=2,lastName = "Lily")

            textView.text = textView.text.toString() + "Copy of user1 is\n" +
                    "ID: ${user1Copy.id}, Name : ${user1Copy.firstName}" +
                    " ${user1Copy.lastName}"
        }

    }



    // Initialize user data class
    data class User(
        val id: Int,
        var firstName:String,
        var lastName: String
    )


    // Data class with properties default values
    data class Customer(
        val id:Int,
        var name: String,
        var city:String="Khulna",
        var age:Int=20
    )
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F8F8F8"
    android:padding="24dp">

    <Button
        android:id="@+id/btnDataClass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data Class Example"
        android:textAllCaps="false"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnDataClassDefault"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Data Class Properties Default Values"
        android:textAllCaps="false"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnDataClass" />

    <Button
        android:id="@+id/btnDataClassCopy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Data Class Copy"
        android:textAllCaps="false"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnDataClassDefault" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:fontFamily="sans-serif"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnDataClassCopy" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android examples