SlideShare a Scribd company logo
Reducing boilerplate w/
Kotlin,
KTX &
Kotlin Android Extensions
author: Patrick P Steiger
Software Engineer @ CESAR
Structure
0. Motivation
1. Kotlin Idioms
2. Kotlin Android Extensions
3. KTX
Motivation
Reduce boilerplate code
… But why?
Motivation
Reduce boilerplate code
… But why?
1. Do the same w/ less typing
Motivation
Reduce boilerplate code
… But why?
2. > LOC, > BUGS!
(Steve McConnell, Code Complete)
Motivation
KISS principle
design principle noted by the U.S. Navy in 1960.
most systems work best if they are kept simple
rather than made complicated; therefore
simplicity should be a key goal in design, and that
unnecessary complexity should be avoided.
Motivation
less is more
Proverb.
That which is less complicated is often better understood and
more appreciated than what is more complicated;
simplicity is preferable to complexity;
brevity in communication is more effective than verbosity.
Motivation
"A designer knows he has achieved perfection not when there is
nothing left to add, but when there is nothing left to take away."
- Antoine de Saint-Exupery
“... a substantial number of the problems caused by buggy software
occurs because vendors keep adding more and more features to their
programs, which inevitably means more code and thus more bugs.”
- Andrew Tannenbaum
Motivation
Kotlin Idioms
00. Substitute returns for assignments
fun sum(a: Int, b: Int): Int {
return a+b
}
fun sum(a: Int, b: Int): Int = a+b
Kotlin Idioms
01. Omit return types
fun sum(a: Int, b: Int): Int = a+b
fun sum(a: Int, b: Int) = a+b
Kotlin Idioms
!!, ? - Kotlin Answer to NPE hell
(it’s safer && terser)
Kotlin Idioms
02. Avoid Java’s excessive null checks
02.0. Define non-nullable vars
val aCounter: Int = 0 // does NOT accept null
aCounter = null // CRASH in compile time (better than run time!)
●Error:(9, 8) Null can not be a value of a non-null type Int
Kotlin Idioms
02. Avoid Java’s excessive null checks
02.1. Define nullable vars, but avoid ‘if’ checks
val aCounter: Int? = 0 // Nullable
aCounter = null // OK!
aCounter?.let { // alternative to if (aCounter != null) {
anotherInt += it // anotherInt += aCounter
} // }
anotherInt += aCounter // OOPS - doesn’t compile - var can be null
anotherInt += aCounter!! // compiles. ‘!!’ means “I’m SURE it’s not null”
Kotlin Idioms
03. Avoid repeating yourself
fun aFunc(aView: View?): View? {
if (aView != null) {
aView.text = “A text”
aView.setBackgroundColor(Color.parseColor(“#000000”))
aView.setElevation(1f)
}
return aView
}
fun aFunc(aView: View?): View? {
aView?.run { // aView becomes ‘this’ if not null
text = “A text” // this.text, this is omitted
setBackgroundColor(Color.parseColor(“#000000”))
setElevation(1f)
}
return aView
}
Kotlin Idioms
03. Avoid repeating yourself
fun aFunc(aView: View?): View? {
if (aView != null) {
aView.text = “A text”
aView.setBackgroundColor(Color.parseColor(“#000000”))
aView.setElevation(1f)
}
return aView
}
fun aFunc(aView: View?) { // omit return type
aView?.run { // aView becomes ‘this’ if not null
text = “A text” // this.text, this is omitted
setBackgroundColor(Color.parseColor(“#000000”))
setElevation(1f)
}
return aView
}
Kotlin Idioms
03. Avoid repeating yourself
fun aFunc(aView: View?): View? {
if (aView != null) {
aView.text = “A text”
aView.setBackgroundColor(Color.parseColor(“#000000”))
aView.setElevation(1f)
}
return aView
}
fun aFunc(aView: View?) = aView?.run { // aView becomes ‘this’ if not null
text = “A text” // this.text, this is omitted
setBackgroundColor(Color.parseColor(“#000000”))
setElevation(1f)
}
Kotlin Idioms
03. Avoid repeating yourself
fun aFunc(aView: View?): View? {
if (aView != null) {
aView.text = “A text”
aView.setBackgroundColor(Color.parseColor(“#000000”))
aView.setElevation(1f)
}
return aView
}
fun aFunc(aView: View?) = aView?.run { // aView becomes ‘this’ if not null
text = “A text” // this.text, this is omitted
setBackgroundColor(Color.parseColor(“#000000”))
setElevation(1f)
}
Kotlin Idioms
03. Avoid repeating yourself - return ‘when’ expressions
fun foo(color: String): Int {
var result: Int = 0
if (color == “Red”) {
result = 10
} else if (color == “Green”) {
result = 11
} else if (color == “Blue”) {
result = 12
}
return result
}
fun foo(color: String) = when (color) {
"Red" -> 10
"Green" -> 11
"Blue" -> 12
else -> 0
}
Kotlin Idioms
03. Avoid repeating yourself - return ‘when’ expressions
fun transform(color: String) {
if (color == “Red”) {
return 0
} else if (color == “Green”) {
return 1
} else if (color == “Blue”) {
return 2
} else {
throw IllegalArgumentException("Invalid color param value”)
}
}
fun transform(color: String) = when (color) {
"Red" -> 0
"Green" -> 1
"Blue" -> 2
else -> throw IllegalArgumentException("Invalid color param value”)
}
Kotlin Idioms
03. Avoid repeating yourself
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getResources().getString(R.string.gps_network_not_enabled));
builder.setPositiveButton(getResources().getString(R.string.ok)), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(Intent(ACTION_LOCATION_SOURCE_SETTINGS));
}
});
builder.show();
AlertDialog.Builder(activity).apply {
setMessage(resources.getString(R.string.gps_network_not_enabled))
setPositiveButton(resources.getString(R.string.ok)) { _, _ ->
startActivity(Intent(ACTION_LOCATION_SOURCE_SETTINGS))
}
show()
}
Kotlin Idioms
03. Avoid repeating yourself
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getResources().getString(R.string.gps_network_not_enabled));
builder.setPositiveButton(getResources().getString(R.string.ok)), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(Intent(ACTION_LOCATION_SOURCE_SETTINGS));
}
});
builder.show();
AlertDialog.Builder(activity).apply {
setMessage(resources.getString(R.string.gps_network_not_enabled))
setPositiveButton(resources.getString(R.string.ok)) { _, _ ->
startActivity(Intent(ACTION_LOCATION_SOURCE_SETTINGS))
}
show()
}
Kotlin Android Extensions
Kotlin Android Extensions
Forget findViewById Forever
Kotlin Android Extensions
on build.gradle:
apply plugin: 'kotlin-android-extensions'
on activity:
import kotlinx.android.synthetic.main.<layout>.*
on fragment:
import kotlinx.android.synthetic.main.<layout>.view.*
Kotlin Android Extensions
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView1 = findViewById(R.id.textView1) as TextView
val textView2 = findViewById(R.id.textView2) as TextView
textView1.setText("Hello")
textView2.setText("world!")
}
}
Kotlin Android Extensions
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView1 = findViewById(R.id.textView1) as TextView
val textView2 = findViewById(R.id.textView2) as TextView
textView1.setText("Hello")
textView2.setText("world!")
}
}
Kotlin Android Extensions
import kotlinx.android.synthetic.main.activity_main.*
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView1 = findViewById(R.id.textView1) as TextView
val textView2 = findViewById(R.id.textView2) as TextView
textView1.setText("Hello")
textView2.setText("world!")
}
}
Kotlin Android Extensions
import kotlinx.android.synthetic.main.activity_main.*
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView1 = findViewById(R.id.textView1) as TextView
val textView2 = findViewById(R.id.textView2) as TextView
textView1.setText("Hello")
textView2.setText("world!")
}
}
Kotlin Android Extensions
What about Fragments?
import kotlinx.android.synthetic.main.the_fragment_layout.view.*
class AppListFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?):
View? = inflater.inflate(R.layout.fragment_app_list, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
welcomeMessage.text = "Hello Kotlin!"
}
}
there is a welcomeMessage id in fragment_app_list XML
Android KTX
“Android KTX is a set of Kotlin extensions that is part of the
Android Jetpack family. It optimizes Jetpack and Android
platform APIs for Kotlin use.”
Android KTX
Project’s build.gradle
repositories {
google()
}
App’s build.gradle
dependencies {
implementation 'androidx.core:core-ktx:1.0.0'
}
Android KTX
animatorInstance.addListener(object: Animator.AnimatorListener {
override fun onAnimationRepeat(animator: Animator?) {
// onRepeat behavior
}
override fun onAnimationEnd(animator: Animator?) {
// onEnd behavior
}
override fun onAnimationCancel(animator: Animator?) {
// onCancel behavior
}
override fun onAnimationStart(animator: Animator?) {
// onStart behavior
}
})
Android KTX
animatorInstance.doOnEnd {
// onEnd behaviour
}
Android KTX
val bundle = Bundle()
bundle.putString(ARG_TEMPLATE_TYPE, templateType.name)
bundle.putString(ARG_TEMPLATE_DESC, templateType.desc)
With KTX:
val bundle = bundleOf( // map syntax
ARG_TEMPLATE_TYPE to templateType.name,
ARG_TEMPLATE_DESC to templateType.desc
)
Android KTX
private fun viewToBitmap(view: View): Bitmap {
// 1 Create a new bitmap with the view info
val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
// 2 Draw the view pixes into the bitmap
val canvas = Canvas(bitmap)
view.draw(canvas)
return bitmap
}
With KTX:
private fun viewToBitmap(view: View) = view.drawToBitmap() // implicit return
Android KTX
if (view.getVisibility() == View.INVISIBLE) // int
view.setVisibility(View.VISIBLE)
With KTX:
if (!view.isVisible) // boolean
view.isVisible = true
Android KTX
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("key1", "value");
editor.putInt("key2", 1);
editor.apply();
With KTX:
import androidx.core.content.edit
getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit {
putString("key1", "value")
putInt("key2", 1)
}
Summing up...
Kotlin wants you to do more and type less
This shows in its nature, syntax and libraries
References
Kotlin Idioms https://kotlinlang.org/docs/reference/idioms.html
Kotlin Android Extensions https://kotlinlang.org/docs/tutorials/android-plugin.html
Android KTX https://developer.android.com/kotlin/ktx
Thank you
contact: psteiger@gmail.com

More Related Content

PDF
Event driven javascript
PPTX
Android & Kotlin - The code awakens #01
PDF
Lean way write asynchronous code with Kotlin’s coroutines - Ronen Sabag, Gett
PDF
Kotlin Generation
PDF
Kotlin: forse è la volta buona (Trento)
PPTX
Java final project of scientific calcultor
PPTX
Scientific calcultor-Java
PDF
Unit Testing in Kotlin
Event driven javascript
Android & Kotlin - The code awakens #01
Lean way write asynchronous code with Kotlin’s coroutines - Ronen Sabag, Gett
Kotlin Generation
Kotlin: forse è la volta buona (Trento)
Java final project of scientific calcultor
Scientific calcultor-Java
Unit Testing in Kotlin

What's hot (20)

PDF
7 Stages of Unit Testing in iOS
PDF
Inversion Of Control
PDF
掀起 Swift 的面紗
PDF
Stubる - Mockingjayを使ったHTTPクライアントのテスト -
PDF
4Developers: Michał Szczepanik- Kotlin - Let’s ketchup it
PDF
Containers & Dependency in Ember.js
PDF
Java SE 8 for Java EE developers
PDF
Get cfml Into the Box 2018
PDF
Event Driven Javascript
PDF
Architectures in the compose world
PDF
Beyond the Callback: Yield Control with Javascript Generators
PPTX
Behind modern concurrency primitives
PPTX
Akka.NET streams and reactive streams
PPTX
Dagger 2. The Right Way to Dependency Injections
PDF
Dagger 2. Right way to do Dependency Injection
PDF
Asynchronous programming done right - Node.js
PPTX
JFokus 50 new things with java 8
PDF
4Developers: Dominik Przybysz- Message Brokers
PPTX
Android Developer Toolbox 2017
PDF
Kotlin Delegates in practice - Kotlin community conf
7 Stages of Unit Testing in iOS
Inversion Of Control
掀起 Swift 的面紗
Stubる - Mockingjayを使ったHTTPクライアントのテスト -
4Developers: Michał Szczepanik- Kotlin - Let’s ketchup it
Containers & Dependency in Ember.js
Java SE 8 for Java EE developers
Get cfml Into the Box 2018
Event Driven Javascript
Architectures in the compose world
Beyond the Callback: Yield Control with Javascript Generators
Behind modern concurrency primitives
Akka.NET streams and reactive streams
Dagger 2. The Right Way to Dependency Injections
Dagger 2. Right way to do Dependency Injection
Asynchronous programming done right - Node.js
JFokus 50 new things with java 8
4Developers: Dominik Przybysz- Message Brokers
Android Developer Toolbox 2017
Kotlin Delegates in practice - Kotlin community conf
Ad

Similar to Reducing boilerplate with Kotlin, KTX and Kotlin Android Extensions (20)

PDF
Kotlin for Android - Vali Iorgu - mRready
PDF
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
PDF
Practical tips for building apps with kotlin
PDF
Save time with kotlin in android development
PDF
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
PPTX
Introduction to kotlin
PPTX
Geecon - Improve your Android-fu with Kotlin
PPTX
Android & Kotlin - The code awakens #02
PDF
Thinking In Swift
PPTX
Why kotlininandroid
PPTX
Building Mobile Apps with Android
PDF
Koin Quickstart
PPTX
01 Introduction to Kotlin - Programming in Kotlin.pptx
PPTX
Introduction to Kotlin.pptx
PDF
Kotlin: Why Do You Care?
PDF
Kotlin for backend development (Hackaburg 2018 Regensburg)
PDF
PDF
What’s new in Kotlin?
PDF
From Java to Kotlin - The first month in practice v2
PDF
Be More Productive with Kotlin
Kotlin for Android - Vali Iorgu - mRready
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
Practical tips for building apps with kotlin
Save time with kotlin in android development
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Introduction to kotlin
Geecon - Improve your Android-fu with Kotlin
Android & Kotlin - The code awakens #02
Thinking In Swift
Why kotlininandroid
Building Mobile Apps with Android
Koin Quickstart
01 Introduction to Kotlin - Programming in Kotlin.pptx
Introduction to Kotlin.pptx
Kotlin: Why Do You Care?
Kotlin for backend development (Hackaburg 2018 Regensburg)
What’s new in Kotlin?
From Java to Kotlin - The first month in practice v2
Be More Productive with Kotlin
Ad

Recently uploaded (6)

PDF
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
DOC
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证
PDF
heheheueueyeyeyegehehehhehshMedia-Literacy.pdf
DOC
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
PDF
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
PPTX
ASMS Telecommunication company Profile
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证
heheheueueyeyeyegehehehhehshMedia-Literacy.pdf
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
ASMS Telecommunication company Profile

Reducing boilerplate with Kotlin, KTX and Kotlin Android Extensions

  • 1. Reducing boilerplate w/ Kotlin, KTX & Kotlin Android Extensions author: Patrick P Steiger Software Engineer @ CESAR
  • 2. Structure 0. Motivation 1. Kotlin Idioms 2. Kotlin Android Extensions 3. KTX
  • 4. Motivation Reduce boilerplate code … But why? 1. Do the same w/ less typing
  • 5. Motivation Reduce boilerplate code … But why? 2. > LOC, > BUGS! (Steve McConnell, Code Complete)
  • 6. Motivation KISS principle design principle noted by the U.S. Navy in 1960. most systems work best if they are kept simple rather than made complicated; therefore simplicity should be a key goal in design, and that unnecessary complexity should be avoided.
  • 7. Motivation less is more Proverb. That which is less complicated is often better understood and more appreciated than what is more complicated; simplicity is preferable to complexity; brevity in communication is more effective than verbosity.
  • 8. Motivation "A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." - Antoine de Saint-Exupery “... a substantial number of the problems caused by buggy software occurs because vendors keep adding more and more features to their programs, which inevitably means more code and thus more bugs.” - Andrew Tannenbaum
  • 10. Kotlin Idioms 00. Substitute returns for assignments fun sum(a: Int, b: Int): Int { return a+b } fun sum(a: Int, b: Int): Int = a+b
  • 11. Kotlin Idioms 01. Omit return types fun sum(a: Int, b: Int): Int = a+b fun sum(a: Int, b: Int) = a+b
  • 12. Kotlin Idioms !!, ? - Kotlin Answer to NPE hell (it’s safer && terser)
  • 13. Kotlin Idioms 02. Avoid Java’s excessive null checks 02.0. Define non-nullable vars val aCounter: Int = 0 // does NOT accept null aCounter = null // CRASH in compile time (better than run time!) ●Error:(9, 8) Null can not be a value of a non-null type Int
  • 14. Kotlin Idioms 02. Avoid Java’s excessive null checks 02.1. Define nullable vars, but avoid ‘if’ checks val aCounter: Int? = 0 // Nullable aCounter = null // OK! aCounter?.let { // alternative to if (aCounter != null) { anotherInt += it // anotherInt += aCounter } // } anotherInt += aCounter // OOPS - doesn’t compile - var can be null anotherInt += aCounter!! // compiles. ‘!!’ means “I’m SURE it’s not null”
  • 15. Kotlin Idioms 03. Avoid repeating yourself fun aFunc(aView: View?): View? { if (aView != null) { aView.text = “A text” aView.setBackgroundColor(Color.parseColor(“#000000”)) aView.setElevation(1f) } return aView } fun aFunc(aView: View?): View? { aView?.run { // aView becomes ‘this’ if not null text = “A text” // this.text, this is omitted setBackgroundColor(Color.parseColor(“#000000”)) setElevation(1f) } return aView }
  • 16. Kotlin Idioms 03. Avoid repeating yourself fun aFunc(aView: View?): View? { if (aView != null) { aView.text = “A text” aView.setBackgroundColor(Color.parseColor(“#000000”)) aView.setElevation(1f) } return aView } fun aFunc(aView: View?) { // omit return type aView?.run { // aView becomes ‘this’ if not null text = “A text” // this.text, this is omitted setBackgroundColor(Color.parseColor(“#000000”)) setElevation(1f) } return aView }
  • 17. Kotlin Idioms 03. Avoid repeating yourself fun aFunc(aView: View?): View? { if (aView != null) { aView.text = “A text” aView.setBackgroundColor(Color.parseColor(“#000000”)) aView.setElevation(1f) } return aView } fun aFunc(aView: View?) = aView?.run { // aView becomes ‘this’ if not null text = “A text” // this.text, this is omitted setBackgroundColor(Color.parseColor(“#000000”)) setElevation(1f) }
  • 18. Kotlin Idioms 03. Avoid repeating yourself fun aFunc(aView: View?): View? { if (aView != null) { aView.text = “A text” aView.setBackgroundColor(Color.parseColor(“#000000”)) aView.setElevation(1f) } return aView } fun aFunc(aView: View?) = aView?.run { // aView becomes ‘this’ if not null text = “A text” // this.text, this is omitted setBackgroundColor(Color.parseColor(“#000000”)) setElevation(1f) }
  • 19. Kotlin Idioms 03. Avoid repeating yourself - return ‘when’ expressions fun foo(color: String): Int { var result: Int = 0 if (color == “Red”) { result = 10 } else if (color == “Green”) { result = 11 } else if (color == “Blue”) { result = 12 } return result } fun foo(color: String) = when (color) { "Red" -> 10 "Green" -> 11 "Blue" -> 12 else -> 0 }
  • 20. Kotlin Idioms 03. Avoid repeating yourself - return ‘when’ expressions fun transform(color: String) { if (color == “Red”) { return 0 } else if (color == “Green”) { return 1 } else if (color == “Blue”) { return 2 } else { throw IllegalArgumentException("Invalid color param value”) } } fun transform(color: String) = when (color) { "Red" -> 0 "Green" -> 1 "Blue" -> 2 else -> throw IllegalArgumentException("Invalid color param value”) }
  • 21. Kotlin Idioms 03. Avoid repeating yourself AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(getResources().getString(R.string.gps_network_not_enabled)); builder.setPositiveButton(getResources().getString(R.string.ok)), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { startActivity(Intent(ACTION_LOCATION_SOURCE_SETTINGS)); } }); builder.show(); AlertDialog.Builder(activity).apply { setMessage(resources.getString(R.string.gps_network_not_enabled)) setPositiveButton(resources.getString(R.string.ok)) { _, _ -> startActivity(Intent(ACTION_LOCATION_SOURCE_SETTINGS)) } show() }
  • 22. Kotlin Idioms 03. Avoid repeating yourself AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(getResources().getString(R.string.gps_network_not_enabled)); builder.setPositiveButton(getResources().getString(R.string.ok)), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { startActivity(Intent(ACTION_LOCATION_SOURCE_SETTINGS)); } }); builder.show(); AlertDialog.Builder(activity).apply { setMessage(resources.getString(R.string.gps_network_not_enabled)) setPositiveButton(resources.getString(R.string.ok)) { _, _ -> startActivity(Intent(ACTION_LOCATION_SOURCE_SETTINGS)) } show() }
  • 24. Kotlin Android Extensions Forget findViewById Forever
  • 25. Kotlin Android Extensions on build.gradle: apply plugin: 'kotlin-android-extensions' on activity: import kotlinx.android.synthetic.main.<layout>.* on fragment: import kotlinx.android.synthetic.main.<layout>.view.*
  • 26. Kotlin Android Extensions class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val textView1 = findViewById(R.id.textView1) as TextView val textView2 = findViewById(R.id.textView2) as TextView textView1.setText("Hello") textView2.setText("world!") } }
  • 27. Kotlin Android Extensions class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val textView1 = findViewById(R.id.textView1) as TextView val textView2 = findViewById(R.id.textView2) as TextView textView1.setText("Hello") textView2.setText("world!") } }
  • 28. Kotlin Android Extensions import kotlinx.android.synthetic.main.activity_main.* class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val textView1 = findViewById(R.id.textView1) as TextView val textView2 = findViewById(R.id.textView2) as TextView textView1.setText("Hello") textView2.setText("world!") } }
  • 29. Kotlin Android Extensions import kotlinx.android.synthetic.main.activity_main.* class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val textView1 = findViewById(R.id.textView1) as TextView val textView2 = findViewById(R.id.textView2) as TextView textView1.setText("Hello") textView2.setText("world!") } }
  • 30. Kotlin Android Extensions What about Fragments? import kotlinx.android.synthetic.main.the_fragment_layout.view.* class AppListFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_app_list, container, false) override fun onViewCreated(view: View, savedInstanceState: Bundle?) { welcomeMessage.text = "Hello Kotlin!" } } there is a welcomeMessage id in fragment_app_list XML
  • 31. Android KTX “Android KTX is a set of Kotlin extensions that is part of the Android Jetpack family. It optimizes Jetpack and Android platform APIs for Kotlin use.”
  • 32. Android KTX Project’s build.gradle repositories { google() } App’s build.gradle dependencies { implementation 'androidx.core:core-ktx:1.0.0' }
  • 33. Android KTX animatorInstance.addListener(object: Animator.AnimatorListener { override fun onAnimationRepeat(animator: Animator?) { // onRepeat behavior } override fun onAnimationEnd(animator: Animator?) { // onEnd behavior } override fun onAnimationCancel(animator: Animator?) { // onCancel behavior } override fun onAnimationStart(animator: Animator?) { // onStart behavior } })
  • 35. Android KTX val bundle = Bundle() bundle.putString(ARG_TEMPLATE_TYPE, templateType.name) bundle.putString(ARG_TEMPLATE_DESC, templateType.desc) With KTX: val bundle = bundleOf( // map syntax ARG_TEMPLATE_TYPE to templateType.name, ARG_TEMPLATE_DESC to templateType.desc )
  • 36. Android KTX private fun viewToBitmap(view: View): Bitmap { // 1 Create a new bitmap with the view info val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888) // 2 Draw the view pixes into the bitmap val canvas = Canvas(bitmap) view.draw(canvas) return bitmap } With KTX: private fun viewToBitmap(view: View) = view.drawToBitmap() // implicit return
  • 37. Android KTX if (view.getVisibility() == View.INVISIBLE) // int view.setVisibility(View.VISIBLE) With KTX: if (!view.isVisible) // boolean view.isVisible = true
  • 38. Android KTX SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); editor.putString("key1", "value"); editor.putInt("key2", 1); editor.apply(); With KTX: import androidx.core.content.edit getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit { putString("key1", "value") putInt("key2", 1) }
  • 39. Summing up... Kotlin wants you to do more and type less This shows in its nature, syntax and libraries
  • 40. References Kotlin Idioms https://kotlinlang.org/docs/reference/idioms.html Kotlin Android Extensions https://kotlinlang.org/docs/tutorials/android-plugin.html Android KTX https://developer.android.com/kotlin/ktx