Slide 1

Slide 1 text

Kotlin & Android Data Binding Library @takuji31

Slide 2

Slide 2 text

@takuji31 (Takuji Nishibayashi) Application Engineer at Hatena

Slide 3

Slide 3 text

Loves Kotlin

Slide 4

Slide 4 text

Kotlin 1.0.1-2 released

Slide 5

Slide 5 text

Android Data Binding Library

Slide 6

Slide 6 text

Official binding library for Android

Slide 7

Slide 7 text

Android Studio support

Slide 8

Slide 8 text

One way binding support

Slide 9

Slide 9 text

Two way binding support latest version

Slide 10

Slide 10 text

Can use with Kotlin

Slide 11

Slide 11 text

Define binding using layout XML

Slide 12

Slide 12 text

Auto generate binding class

Slide 13

Slide 13 text

Define binding 
 
 
 
 
 
 
 
 
 
 
 


Slide 14

Slide 14 text

Using binding class DataClassActivity : AppCompatActivity() {
 data class User(val name: String, val birthDay: String?)
 var users = listOf(
 User(name = "takuji31", birthDay = "1987/03/01"),
 User(name = "takuji32", birthDay = "1987/03/02"),
 User(name = "takuji33", birthDay = "1987/03/03"),
 User(name = "takuji24884", birthDay = null)
 )
 val binding: ActivityDataClassBinding by lazy {
 DataBindingUtil.setContentView(this, R.layout.activity_data_class)
 }
 override fun onCreate(savedInstanceState: Bundle?) {
 binding.user = users.last()
 binding.randomButtonClickListener = View.OnClickListener {
 binding.user = users[0]
 users = users.drop(1) + binding.user
 }
 }
 }

Slide 15

Slide 15 text

No more (ViewClass)findById(R.id.viewId)

Slide 16

Slide 16 text

Define binding 
 
 


Slide 17

Slide 17 text

Field access class MainActivity : AppCompatActivity() {
 val binding : ActivityMainBinding by lazy {DataBindingUtil.setContentView(this, R.layout.activity_main)} 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.recyclerView.layoutManager = LinearLayoutManager(this)
 }
 }

Slide 18

Slide 18 text

Property observer

Slide 19

Slide 19 text

Observable data class class Counter : BaseObservable() {
 @get:Bindable
 var count : Int = 0
 set(value) {
 field = value
 notifyPropertyChanged(BR.count)
 }
 }


Slide 20

Slide 20 text

Define binding 
 
 
 
 
 
 
 
 
 


Slide 21

Slide 21 text

Change value class PropertyObserverActivity : AppCompatActivity() {
 val binding : ActivityPropertyObserverBinding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_property_observer ) } 
 override fun onCreate(savedInstanceState: Bundle?) {
 val counter = Counter()
 binding.counter = counter
 binding.buttonClickListener = View.OnClickListener {
 counter.count += 1 }
 }
 }

Slide 22

Slide 22 text

Binding adapter

Slide 23

Slide 23 text

Define binding adapter object Adapters {
 @JvmStatic
 @BindingAdapter("android:text")
 fun convertZonedDateTimeToString (textView: TextView, zonedDateTime : ZonedDateTime) {
 val timeString = zonedDateTime.format( DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm:SS"))
 textView.text = timeString
 }
 }

Slide 24

Slide 24 text

Value converter

Slide 25

Slide 25 text

Issues with Kotlin

Slide 26

Slide 26 text

Can’t find converter written with Kotlin

Slide 27

Slide 27 text

Use Java

Slide 28

Slide 28 text

example : github.com/ takuji31/KotlinDataBinding

Slide 29

Slide 29 text

Enjoy Data Binding Life