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 myStringArray = Array(3){""}
// Initialize a string array with default value and type
var myStringArray2 = Array<String>(3){""}
// Initialize a new int array with default value
val myIntArray = Array(5){6}
// Initialize a new Int array with values
var myIntArray2:IntArray = intArrayOf(1,2,3,4)
// Initialize a double array with default value
var myDoubleArray = Array<Double>(3){10.0}
// Initialize a float array with default value
var myFloatArray = Array<Float>(2){10.0F}
// Display the first array elements in text view
textView.text = "First Array elements\n"
// Loop through the first array elements
for (element in myArray){
textView.text = textView.text.toString() + element + ", "
}
// Display the int array elements in text view
textView.text = textView.text.toString() +
"\n\nInt array elements\n"
// Loop through the first array elements
for (element in myIntArray){
textView.text = textView.text.toString() + element + ", "
}
// Updating a default value of integer array
myIntArray[2] = 10
// After update displaying the int array elements in text view
textView.text = textView.text.toString() +
"\n\nAfter updating, int array elements\n"
// Loop through the first array elements
for (element in myIntArray){
textView.text = textView.text.toString() + element + ", "
}
// Sum of int array elements
textView.text = textView.text.toString() +
"\n\nSum of int array elements is " +
": ${myIntArray.sum()}\n"
// Average of int array elements
textView.text = textView.text.toString() +
"\n\nAverage of int array elements is " +
": ${myIntArray.average()}\n"
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F8F8F8"
android:padding="24dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Array"
android:textAllCaps="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

- kotlin - If else example
- kotlin - Function example
- kotlin - Function parameter default value example
- kotlin - Data class example
- kotlin - Vibrate phone programmatically
- kotlin - Android Palette API Example
- kotlin - Enum example
- kotlin - HashMap example
- kotlin - File provider image share internal storage
- kotlin - Room database example