Sunday, January 28, 2018

android kotlin - Array example

MainActivity.kt

package com.cfsuman.kotlinexamples

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        // Button click listener
        button.setOnClickListener {

            // Initialize a new array with values
            var 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
            var 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
            text_view.text = "First Array elements\n"

            // Loop through the first array elements
            for (element in myArray){
                text_view.text = text_view.text.toString() + element + ", "
            }


            // Display the int array elements in text view
            text_view.text = text_view.text.toString() +"\n\nInt array elements\n"

            // Loop through the first array elements
            for (element in myIntArray){
                text_view.text = text_view.text.toString() + element + ", "
            }


            // Updating a default value of integer array
            myIntArray[2] = 10

            // After update displaying the int array elements in text view
            text_view.text = text_view.text.toString() +"\n\nAfter updating, int array elements\n"

            // Loop through the first array elements
            for (element in myIntArray){
                text_view.text = text_view.text.toString() + element + ", "
            }


            // Sum of int array elements
            text_view.text = text_view.text.toString() +"\n\nSum of int array elements is : ${myIntArray.sum()}\n"


            // Average of int array elements
            text_view.text = text_view.text.toString() +"\n\nAverage of int array elements is : ${myIntArray.average()}\n"
        }
    }
}
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="Array Example"
        />
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:layout_marginTop="25dp"
        android:padding="15dp"
        android:textColor="#224acc"
        android:textStyle="bold"
        />
</LinearLayout>
More android examples