Thursday, January 25, 2018

android kotlin - Function parameter default value example

MainActivity.kt

package com.cfsuman.kotlinexamples

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.TypedValue
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        // Test function with optional parameter and default value
        button.setOnClickListener{
            showMessage("Hello Android Development!")
        }

        // Here we pass value to optional parameter
        button2.setOnClickListener{
            showMessage("Hello New Color!", Color.BLUE)
        }

        // Here we calling function using parameter name
        button3.setOnClickListener{
            showMessage(message = "Hello Parameter Name!", color = Color.MAGENTA)
        }

        // Here we calling a function which have multiple optional parameters and values
        button4.setOnClickListener{
            styledMessage(message = "Hello styled message!",textColor = Color.WHITE,bgColor = Color.GREEN)
        }

        // Here we calling a function which have multiple optional parameters and values
        // We calling this function using parameter name and we break parameter sequence
        button5.setOnClickListener{
            styledMessage(message = "Hello Text Size!",textSize = 30F,bgColor = Color.CYAN)
        }
    }


    // Function optional parameter and default value
    fun showMessage(message:String, color:Int = Color.RED): Unit{
        text_view.text = message
        text_view.setTextColor(color)
    }


    // Function with multiple optional parameters and default values
    fun styledMessage(message: String, textColor:Int=Color.DKGRAY, bgColor:Int = Color.YELLOW, textSize:Float = 25F){
        text_view.text = message;
        text_view.setTextColor(textColor)
        text_view.setBackgroundColor(bgColor)
        text_view.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSize)
    }
}
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="Parameter Default Value"
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Optional Parameter Provided Value"
        />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Use Parameter Name When Calling Function"
        />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Multiple Optional Parameters"
        />
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Another Multiple Optional Parameters"
        />
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="Hello Kotlin!"
        android:layout_marginTop="25dp"
        android:padding="15dp"
        />
</LinearLayout>