Tuesday, January 23, 2018

android kotlin - When statement example

MainActivity.kt

package com.cfsuman.kotlinexamples

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


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.text_view)
        val button = findViewById<Button>(R.id.button);
        val button2 = findViewById<Button>(R.id.button2);
        val button3 = findViewById<Button>(R.id.button3);


        // When statement example
        button.setOnClickListener{
            // Initialize an integer type variable
            var number:Int = 3

            when(number){
                1 -> textView.text = "Value is 1"
                2 -> textView.text = "Value is 2"
                3 -> textView.text = "Value is 3"
                else -> textView.text = "Value is not between 1 and 3"
            }
        }


        // When statement example
        button2.setOnClickListener{
            // Initialize an integer type variable
            var number:Int = 4

            when(number){
                0,1 -> textView.text = "Value is 0 or 1"
                2 -> textView.text = "Value is 2"
                3,4,5 -> textView.text = "Value is 3 or 4 or 5"
                else -> textView.text = "Value is not between 0 and 5"
            }
        }


        // When statement example
        button3.setOnClickListener{
            // Initialize an integer type variable
            var number:Int = 51

            when(number){
                in 1..10 -> textView.text = "Value is between 1 and 10"
                !in 11..50 -> textView.text = "Value is not between 10 and 50"
                else -> textView.text = "Value is unknown"
            }
        }
    }
}
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="When statement"
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Another When"
        />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="When Again"
        />
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#ff191d"
        android:layout_marginTop="20dp"
        />
</LinearLayout>