Slide 1

Slide 1 text

findViewById Kotlin Android Extensions Edition Juliane Lehmann

Slide 2

Slide 2 text

The plain way lateinit var titleText: TextView lateinit var empty: View fun onCreate(...) { titleText = findViewById(R.id.title_text) empty = findViewById(R.id.empty) } Access as: titleText empty Walks the view hierarchy to find view with matching id

Slide 3

Slide 3 text

The butterknife way @BindView(R.id.title_text) lateinit var titleText: TextView @BindView(R.id.empty) lateinit var empty: View Butterknife.bind(this) Access as: titleText empty Generated injector code (“Butterknife is like a dagger, only infinitely less sharp”) Can also bind listeners, and other resources

Slide 4

Slide 4 text

The databinding way import my.project.databinding.MainActivityBinding private lateinit var viewBinding: MainActivityBinding viewBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) Access as: viewBinding.title_text viewBinding.empty But mostly, instead, use databinding how it is actually meant (access data object via DSL from layout XML).

Slide 5

Slide 5 text

The Kotlin Android Extensions way import kotlinx.android.synthetic.main.activity_main.* Access as title_text empty

Slide 6

Slide 6 text

The Kotlin Android Extensions way import kotlinx.android.synthetic.main.activity_main.* Access as title_text empty Also: import kotlinx.android.synthetic.main.view_listitem.view.* fun bindItem(...) { itemView.title_text ... itemView.empty … }

Slide 7

Slide 7 text

Simple magic explained Reminder: extension properties exist file ListItemLayout: val View.title_text: TextView get() = this.findViewById(R.id.title_text)

Slide 8

Slide 8 text

Simple magic explained Reminder: extension properties exist file ListItemLayout: val View.title_text: TextView inline get() = this.findViewById(R.id.title_text)

Slide 9

Slide 9 text

Simple magic explained Reminder: extension properties exist file ListItemLayout: val View.title_text: TextView inline get() = this.findViewById(R.id.title_text) This is basically what you get with import kotlinx.android.synthetic.main.activity_main.view.*

Slide 10

Slide 10 text

Simple magic explained Reminder: extension properties exist file ListItemLayout: val View.title_text: TextView inline get() = this.findViewById(R.id.title_text) This is basically what you get with import kotlinx.android.synthetic.main.activity_main.view.* Great for view holders, (fragments)

Slide 11

Slide 11 text

Code!

Slide 12

Slide 12 text

More magic import kotlinx.android.synthetic.main.activity_main.* (without view) instead inlines accessing a generated cache

Slide 13

Slide 13 text

More magic import kotlinx.android.synthetic.main.activity_main.* (without view) instead inlines accessing a generated cache … which gets automatically cleared in Fragment.onDestroyView

Slide 14

Slide 14 text

More magic import kotlinx.android.synthetic.main.activity_main.* (without view) instead inlines accessing a generated cache … which gets automatically cleared in Fragment.onDestroyView Great for activities, custom views, (fragments) - with experimental flag, also works for any class implementing kotlinx.android.extensions.LayoutContainer

Slide 15

Slide 15 text

Code!

Slide 16

Slide 16 text

The crib sheet apply plugin: ‘kotlin-android-extensions’ For containers - Activity, Fragment, View, (LayoutContainer): import kotlinx.android.synthetic.$buildType.$layout_file_name.* (cached extension properties on the container) For inlined access on child views: import kotlinx.android.synthetic.$buildType.$layout_file_name.view.* (extension properties on View)

Slide 17

Slide 17 text

Links ● Sample project on github: https://github.com/strooooke/KotlinAndroidExtensionsExample ● codegen part of Kotlin Android Extensions plugin: https://github.com/JetBrains/kotlin/tree/7da6ff78c10692b46a25596fb8ad256 51ee4dfd2/plugins/android-extensions/android-extensions-compiler/src/org/je tbrains/kotlin/android/synthetic/codegen