This is a Kotlin app that uses Kotlin language features, as
I discover them. This might be an extended post.
package com.example.regan.myapplication import android.os.Bundle import android.support.design.widget.FloatingActionButton import android.support.design.widget.Snackbar import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import android.view.View import android.view.Menu import android.view.MenuItem class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toolbar = findViewById(R.id.toolbar) as Toolbar setSupportActionBar(toolbar) val fab = findViewById(R.id.fab) as FloatingActionButton fab.setOnClickListener { view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } } override fun onCreateOptionsMenu(menu: Menu): Boolean { // Inflate the menu; this adds items to the action bar if it is present. menuInflater.inflate(R.menu.menu_main, menu) return true } override fun onOptionsItemSelected(item: MenuItem): Boolean { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. val id = item.itemId if (id == R.id.action_settings) { return true } return super.onOptionsItemSelected(item) } } class kotlinTest { // val is like let in swift or const in C++ // var is mutable // function without braces. fun sum(a: Int, b: Int) = a + b // Unit is like void return type. ${ } is like \( ) in swift. fun printSum(a: Int, b: Int): Unit { println("sum of $a and $b is ${a + b}") } fun temp() { // String templates var a = 1 // simple name in template: val s1 = "a is $a" a = 2 // arbitrary expression in template: val s2 = "${s1.replace("is", "was")}, but now is $a" } // Using if as an expression: soft of like ?; in Objective-C (?) fun maxOf(a: Int, b: Int) = if (a > b) a else b fun getStringLength(obj: Any): Int? { if (obj is String) { // `obj` is automatically cast to `String` in this branch return obj.length } // `obj` is still of type `Any` outside of the type-checked branch return null } // functions can return optionals. Like swift optionals. fun parseInt(str: String): Int? { // ... // For loop on each item.. val items = listOf("apple", "banana", "kiwi") for (item in items) { println(item) } // For loop on indexes for (index in items.indices) { println("item at $index is ${items[index]}") } // while loop like other langauges var index = 0 while (index < items.size) { println("item at $index is ${items[index]}") index++ } return null } fun describe(obj: Any): String = when (obj) { 1-> "One" "Hello" -> "Greeting" is Long -> "Long" !is String -> "Not a string" else -> "Unknown" }
fun xyz() { // iteration over a range for (x in 1..5) { print(x) } for (x in 9 downTo 0 step 3) { print(x) } // Lambda expressions over val fruits = listOf("apple", "banana", "kiwi") fruits .filter { it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) } // Filtering a list val list = listOf<Int>( 0, 1, 2, 3, 4 ,5 ) val positives = list.filter { x -> x > 0 } var hijk = null when (hijk) { is Int -> print( "int" ) is String -> print( "String" ) else -> { print("unknown") } } // map.. var map = mapOf("a" to 1, "b" to 2, "c" to 3) val value = 0 println(map["key"]) // map["key"] = value // val p: String by lazy { // // compute the string // // } // class File { } // val files = File("Test").listFiles() // println(files?.size ?: "empty") fun test() { val result = try { // count1() } catch (e: ArithmeticException) { throw IllegalStateException(e) } // Working with result } fun count1() { } // If expression, result gets whichever string.. fun foo(param: Int) { val result = if (param == 1) { "one" } else if (param == 2) { "two" } else { "three" } } } // Singletons object Resource { val name = "Name" }
}