This article is a part of a series of articles on Kotlin. Before continuing with this, please have a look at my previous articles.
- Android Kotlin - Hello World - Part One
- Android Kotlin-Variables, Data Types and If-Else, When Statements - Part Two
- Android Kotlin - Strings and Ranges - Part Three
Step 1
Let’s add a class called Employee. Right-click on package and click New >> Kotlin File/Class. Name it Employee and click OK.
Step 2
Create two variables - name and age - in Employee class.
In Kotlin, we have to assign some values either empty or null; otherwise, Kotlin compiler gives an error.
So, it looks like the following.
- class Employee {
- var name: String = ""
- var age: Int = 0
- }
Step 3
In MainActivity.kt, let's create an object of Employee class.
- var employee = Employee();
Step 4
Let’s add two TextViews in activity_main.xml.
- <?xml version="1.0" encoding="utf-8"?> ,
- <android.support.constraint.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"
- tools:context="com.example.kotlin_helloworld.MainActivity">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
-
- <TextView
- android:id="@+id/txtEmpName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text=""
- android:textSize="20dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <TextView
- android:id="@+id/txtEmpAge"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text=""
- android:textSize="20dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </LinearLayout>
- </android.support.constraint.ConstraintLayout>
Step 5
In MainActivity.kt, we can set values to the TextViews,
- var employee = Employee();txtEmpName.setText("Employee Name = ${employee.name}")
- txtEmpAge.setText("Employee Age = ${employee.age}")
Step 6
Run the application and you can see the following output. Since their default values are empty, they are displayed as below.
Step 7
Let’s add a function to set the values for the Employee class.
In Employee.kt, add the following function,
- fun setEmployee(empName: String, empAge: Int) {
- name = empName
- age = empAge
- }
So, from MainActivity.kt, we can call this function.
MainActivity.kt will look like,
- override fun onCreate(savedInstanceState: Bundle ? ) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- var employee = Employee();
- employee.setEmployee("Kishor", 23)
- txtEmpName.setText("Employee Name = ${employee.name}")
- txtEmpAge.setText("Employee Age = ${employee.age}")
- }
Step 8
Run the application and you can see the following output.
Step 9
One unique feature in Kotlin is that the parameters you pass in function are not necessary to be in order. While calling the setEmployee function from MainActivity.kt, we can pass parameters like below.
- employee.setEmployee( empAge = 23, empName = "Kishor")
This will give the same output.
Step 10
If you want to assign some null values to the variables, then you can do as below.
- var name: String? = null
- var age: Int? = null
When you run the application, again, this will give the same result.
Get the project from GitHub.
Thanks, and happy coding.