Android Development using Kotlin
Empty Image
Kotlin is clean and elegant
when (stream) {
is Reader -> stream.close()
is Writer -> stream.close()
is InputStream -> stream.close()
is OutputStream -> stream.close()
is Socket -> stream.close()
else -> System.err.println("Unable to close object: " + stream)
}
is Reader -> stream.close()
is Writer -> stream.close()
is InputStream -> stream.close()
is OutputStream -> stream.close()
is Socket -> stream.close()
else -> System.err.println("Unable to close object: " + stream)
}
Kotlin is expressive
val people = hashMapOf<String, Int>()
for ((person, age) in people) {
println("${person} in ${age} years old")
}
for ((person, age) in people) {
println("${person} in ${age} years old")
}
Functions are very powerful
fun Int.abs() : Int = if (this >= 0) this else -this
Fore more details about Kotlin, visit http://kotlinlang.org/
Hello Android using Kotlin and Android Studio
This tutorial is based on:
- Android Studio (Beta) v0.8.2
Step 1: Install Kotlin plugin, then restart Android studio
Empty Image
Empty Image
Empty Image
Empty Image
Empty Image
Empty Image
Empty Image
Step 2: create a new Android studio project with a blank activity
Empty Image
Empty Image
Empty Image
Empty Image
Empty Image
Step 3: Open main Activity and delete everything in the file except oncreate
Empty Image
Step 4: Rename java dir to kotlin
Empty Image
Empty Image
Step 5: convert java activity file to kotlin
Empty Image
Step 6: change oncreate's argument from bundle to bundle? (i.e. nullable) in the generated file
Empty Image
Step 7: convert project to kotlin (this modifies build.gradle)
Empty Image
Step 8: select v0.8.11
Empty Image
Empty Image
Step 9: Compile project. Now are you good to go.
Empty Image