# Wrap-up "Kotlin for Android Developers" at Udacity
Personally, I want to wrap-up the course to make a summary
- Link: https://www.udacity.com/course/kotlin-for-android-developers--ud888
I learned Kotlin for Android quickly with example.
I don't think this course shows all of Kotlin.
But it's effective to learn some important points and functions in Android Studio.
1. Import Kotlin plugin in the project
- Menu > "Tools" > "Kotlin" > "Configure Kotlin in project" > "Android with Gradle"
This function modifies gradle files to add Kotlin plugin.
- Result: https://github.com/ndukwon/kotlin-notepad/commit/9484b0a9e04b95242544b2a83ca656ab64476114
2. Convert Java file to Kotlin file
- Menu > "Code" > "Convert Java File to Kotlin File"
This function converts a Java file to a Kotlin file.
- Result: https://github.com/ndukwon/kotlin-notepad/commit/f61f09a603b02dcd15bd51deb78fef699fc5181a
This case is lucky. Normally, there are some errors about
+ Nullable check
+ Lambda conversions
+ other non-standard Java features
3. Use SAM Conversions
- SAM: Single Abstract Method
- Methods like "View.OnClickListener.OnClick()" can be applied.
ex) fab.setOnClickListener { startActivity(CreateActivity.get(this@MainActivity)) }
- Result: https://github.com/ndukwon/kotlin-notepad/commit/ad3637101c5f8458187563e43fb548a39e819f96
4. Access views directly without "findViewById()"
- Add "kotlin-android-extensions" plugin to support this
- Import "kotlinx.android.synthetic.~~"
ex) import kotlinx.android.synthetic.main.activity_main.*
You can have direct accesses the views in "R.layout.activity_main".
- Result: https://github.com/ndukwon/kotlin-notepad/commit/ad3637101c5f8458187563e43fb548a39e819f96
5. Convert Java's "switch" to Kotlin's "when"
- "when" looks like "switch" without typing "case" keyword
and supports
+ string comparison
+ "is" keyword
+ return value
- Result: https://github.com/ndukwon/kotlin-notepad/commit/ad3637101c5f8458187563e43fb548a39e819f96
6. Insert a method/variable in the class
- "kotlin-android-extensions" can make extension codes simple.
In compile time, static method/variable is inserted for this.
ex) val Context.layoutInflater get() = LayoutInflater.from(this)
==> So, we can use this like this.
val view = context.layoutInflater.inflate(R.layout.item_note, parent, false)
- Result: https://github.com/ndukwon/kotlin-notepad/commit/fb586664b645c847e79678b81dc4741fa2e5e2c3
7. Use "data class" instead of just "class"
- to support automatically
+ Identity: equal()
+ Copy: copy()
+ Hash
- Result: https://github.com/ndukwon/kotlin-notepad/commit/47aeae37c7488b7577bc7a5713a83753ed31f534
8. Add Anko library
- Anko library contains a lot of helpers for Android SDK.
In this trial, we will use "doAsync()" instead of "AsyncTask"
- Result: https://github.com/ndukwon/kotlin-notepad/commit/ec2d1fc3a3faa520f3d3b2c71b063bfb03af5a61
9. Use "doAsync(): instead of "AsyncTask"
- handles thread pooling and execution
ex) doAsync { runnable.run() }
- Result: https://github.com/ndukwon/kotlin-notepad/commit/e28010bf0514844d5455daccbd0febcf7031f5ba
10. Apply "lateinit" keyword
- to initialize it by the time to be used
ex) lateinit var notes: NoteDatabase
- should not be nullable: cleaner and safer
- Result: https://github.com/ndukwon/kotlin-notepad/commit/e28010bf0514844d5455daccbd0febcf7031f5ba
11. Add "@JvmStatic"
- to help Java file understand getter/setter will be generated automatically
- more: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-static/index.html
- Result: https://github.com/ndukwon/kotlin-notepad/commit/e28010bf0514844d5455daccbd0febcf7031f5ba
To be updated....
Reference:
- https://www.udacity.com/course/kotlin-for-android-developers--ud888
- https://thdev.tech/kotlin/2016/08/02/Basic-Kotlin-01.html