Wednesday, January 24, 2018

android kotlin - Function example

MainActivity.kt

package com.cfsuman.kotlinexamples

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast


class MainActivity : AppCompatActivity() {

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

        // Get the widgets reference from xml layout
        val button = findViewById<Button>(R.id.button);
        val button2 = findViewById<Button>(R.id.button2);
        val button3 = findViewById<Button>(R.id.button3);
        val button4 = findViewById<Button>(R.id.button4);


        // Function test with no argument and void return
        button.setOnClickListener{
            sayHello()
        }


        // Function test with an argument
        button2.setOnClickListener{
            showToast("This is a simple toast.")
        }


        // Function test with arguments and return value
        button3.setOnClickListener{
            var sum = sum(5,10)
            Toast.makeText(applicationContext,"Sum is $sum",Toast.LENGTH_SHORT).show()
        }


        // Function without {} curly braces
        // Function returns single expression
        button4.setOnClickListener{
            var fullName: String = getFullName("Suhailah", "Alam")
            Toast.makeText(applicationContext,fullName,Toast.LENGTH_SHORT).show()
        }
    }


    // Function with no argument and no return value
    fun sayHello(): Unit{
        Toast.makeText(applicationContext,"Hello Kotlin!",Toast.LENGTH_SHORT).show()
    }


    // Function with an argument and return type void
    fun showToast(message: String){
        Toast.makeText(applicationContext,message,Toast.LENGTH_SHORT).show()
    }


    // Function with arguments and return value
    fun sum(a: Int, b: Int):Int{
        return a+b
    }


    // Single line expression function
    fun getFullName(first: String, last: String): String = "$first $last"
}
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="Function + No Argument + Void"
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Function + Argument + Void"
        />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Function + Argument + Return"
        />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Single Line Function"
        />
</LinearLayout>