findViewById() vs Android Kotlin Extensions

Image credit: Inverse

Ways to Implement Kotlin Android Extensions

apply plugin: 'kotlin-android-extensions'

Android Extensions for Activities or Fragments

//activity_login is our layout xml
import
kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.content_main.*

Android Extensions for Views

import kotlinx.android.synthetic.main.view_item.view.*
itemView.textView.text = "I love programming!"
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/forecastList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
import kotlinx.android.synthetic.main.activity_main.*

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_todolist)
forecastList.layoutManager = LinearLayoutManager(this)
...
}
import kotlinx.android.synthetic.main.item_todolist.view.*
class ViewHolder(view: View, val itemClick: (ToDo) -> Unit) :
RecyclerView.ViewHolder(view) {
fun bindToDo(todo: ToDo) {
with(todo) {
itemView.title.text = title
itemView.description.text = description
itemView.dateCreated.text = "$datecreated"
itemView.expiresOn.text = "$expiresOn"
itemView.setOnItemClickListener { itemClick(this) }
}
}
}
Image credit: Giphy

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store