Upgrade to Pro — share decks privately, control downloads, hide ads and more …

findViewById

 findViewById

Introduction to the Kotlin Android Extensions.
by Juliane Lehmann
presented on October 26, 2017 at @Magicline

Kotlin User Group Hamburg

October 26, 2017
Tweet

More Decks by Kotlin User Group Hamburg

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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).
  4. 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 … }
  5. Simple magic explained Reminder: extension properties exist file ListItemLayout: val

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

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

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

    View.title_text: TextView inline get() = this.findViewById<TextView>(R.id.title_text) This is basically what you get with import kotlinx.android.synthetic.main.activity_main.view.* Great for view holders, (fragments)
  9. More magic import kotlinx.android.synthetic.main.activity_main.* (without view) instead inlines accessing a

    generated cache … which gets automatically cleared in Fragment.onDestroyView
  10. 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
  11. 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)
  12. 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