Friday, January 26, 2018

android kotlin - Data class example

MainActivity.kt

package com.cfsuman.kotlinexamples

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

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

            // Display the first user's data in the text view
            text_view.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
            text_view.text = text_view.text.toString() + "\n\nAfter update, now first user is\n"
            text_view.text = text_view.text.toString() + "ID : ${firstUser.id} \nName : ${firstUser.firstName} ${firstUser.lastName}"
        }


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

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

            // Using property name
            var secondCustomer = customer(id =2, age=30, name="Raymond")

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


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

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

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

            text_view.text = text_view.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"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    >
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data Class Example"
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data Class Properties Default Values"
        />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data Class Copy"
        />
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:layout_marginTop="25dp"
        android:padding="15dp"
        android:textColor="#224acc"
        android:textStyle="bold"
        />
</LinearLayout>