Android Kotlin - How to Implement When Statement
This code demonstrates the usage of the when statement in Kotlin for an Android application. The code is implemented within the MainActivity.kt file and interacts with the layout defined in activity_main.xml.
The when statement acts as a concise alternative to multi-branched if-else statements. It evaluates an expression against a series of conditions and executes the corresponding code block for the first matching condition. Let's explore the code further to understand how it utilizes the when statement for different scenarios.
Breakdown of the Code
The MainActivity.kt file defines three buttons and a text view. Clicking each button triggers a separate when statement example.
Button 1: Initializes an integer
numberwith the value 3. Thewhenstatement then checks for specific values:- If
numberis 1, 2, or 3, the text view displays the corresponding message. - Otherwise, it displays a message indicating the value is not between 1 and 3.
- If
Button 2: Similar to the first button, it uses the
whenstatement withnumberset to 4. However, this example showcases combining conditions:- If
numberis 0 or 1, a specific message is displayed. - The logic extends to handle ranges using commas: 3, 4, or 5 all trigger their respective message.
- Any other value results in a message indicating the value is not between 0 and 5.
- If
Button 3: This example demonstrates range checks with the
inoperator:- If
numberfalls between 1 and 10 (inclusive), a message displays its range. - The
!inoperator is used to check ifnumberis not between 11 and 50 (inclusive). - If neither condition matches, a message indicates an unknown value.
- If
Summary
This code effectively demonstrates the versatility of the when statement in Kotlin for Android development. It provides a clean and readable way to handle multiple conditions compared to traditional if-else structures. The examples showcase checking for specific values, combining conditions, and utilizing range checks for a more concise and maintainable codebase.
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"
}
}
}
}
<?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>

