Skip to main content

Posts

android kotlin - Array example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity 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.textView) val button = findViewById<Button>(R.id.button) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Button click listener button.setOnClickListener { // Initialize a new array with values val myArray = arrayOf("Rose",1,"Pink",9.0, 8.5F) // Initialize a string array with default value var...

android kotlin - Data class example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity 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.textView) val btnDataClass = findViewById<Button>(R.id.btnDataClass) val btnDataClassDefault = findViewById<Button>( R.id.btnDataClassDefault) val btnDataClassCopy = findViewById<Button>(R.id.btnDataClassCopy) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Data class example btnDataClass.setOnClickListener{ ...

android kotlin - Function parameter default value example

MainActivity.kt package com.cfsuman.kotlintutorials import android.graphics.Color import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.util.TypedValue import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private lateinit var textView:TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout textView = findViewById(R.id.textView) 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) val button5 = findViewById<Button>(R.id.button5) // make textview content scrollable t...

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 arg...

Android kotlin: How to implement if else statement

Android Kotlin - Understanding if else statements This code demonstrates various ways to implement conditional logic using if statements in Kotlin for an Android application. The code utilizes buttons to trigger different functionalities related to finding the largest number among predefined variables. The TextView element displays the results. The code explores four approaches: Traditional if statement if with else statement if else if statement if as expression Each button click triggers a specific code block that calculates the biggest number between two variables ( a and b ) or three variables ( a , b , and c ) and displays the result on the TextView. Summary By understanding these different if statement implementations, developers can choose the most appropriate approach for their specific needs. For simple conditions, a single if statement might suffice. However, for more complex scenarios with multiple possibilities, if else if statements provide a structured way t...

Android kotlin: How to implement When Statement

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 number with the value 3. The when statement then checks for specific values: If number is 1, 2, or 3, the text view displays the corresponding message. Otherwise, it displays a message indicating the valu...