Passing +id attribute instead of using findViewById

My function is declared:

    fun myFun(imageView: ImageView) {

The ImageView has this attribute defined in layout file my_activity.xml

    android:id="@+id/image_view"

And I am able to invoke the function like so

    myFun(image_view)

I don’t recall ever being able to do that in Java, nor have I been able to find any documentation on this. Can someone please explain what is going on? Did I just stumble upon another Kotlin feature?

EDIT: I just noticed the following import has something to do with it:

import kotlinx.android.synthetic.main.my_activity.*

It’s indeed a kotlin feature, but I guess you already knew it after searching for “synthetic android kotlin” :wink:

https://kotlinlang.org/docs/tutorials/android-plugin.html

https://antonioleiva.com/kotlin-android-extensions/

1 Like