Search code examples
kotlinandroid-databindingandroidxandroid-binding-adapter

AndroidX with BindingAdapter


I'm having some issues getting my BindingAdapter to work in my new app.

I'm always getting the following error:

****/ data binding error ****msg:Cannot find the setter for attribute 'visible' with parameter type boolean

This is my BindingAdapter:

object BindingAdapters {

@set:BindingAdapter("visible")
@JvmStatic
var View.visible
    get() = visibility == View.VISIBLE
    set(value) {
        visibility = if (value) View.VISIBLE else View.GONE
    }
}

This is the view having the view in XML generating the problem:

<TextView
        android:id="@+id/tvDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/item_horizontal_margin_large"
        android:text="@{viewModel.item.description}"
        visible="@{viewModel.showGroup}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@id/ctvDivision"
        app:layout_constraintTop_toTopOf="parent" />

This is a new project, so I started with Android X and didn't need to do the migration.

I have this line in the build.grade:

annotationProcessor 'androidx.databinding:databinding-compiler:3.4.0-alpha09'

and I'm using gradle 3.2.11.

I'm not sure if this is needed, but this is the code where I'm inflating my binding:

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    setHasOptionsMenu(true)

    binding = DataBindingUtil.inflate(
            layoutInflater,
            R.layout.fragment_good,
            container,
            false
    )

    val view = binding?.root
    binding?.viewModel = viewModel

    return view
}

All the other standard databinding stuff works. As soon as I try setting up a BindingAdapter it fails.

I've read on SO that some people claimed databinding didn't work properly with X. But those were posts from october, and there were people claiming it worked perfectly for them in the same post.

Am I forgetting something stupid or is it really not yet working with X?


Solution

  • I reproduce the problem in small new project using Kotlin (1.3.30), AndroidX, DataBinding and Gradle 3.4.0

    Firstly, Kotlin doesn't work with the annotationProcessor and must be replaced by Kotlin Annotation Processing Tool: kapt with kotlin-kapt plugin.
    Kotlin documentation

    Then in Gradle 3.1.0-alpha06, Google has included a new version of the data binding compiler enable by default since Gradle 3.2.
    So you don't need to use databinding-compiler dependency (it is integrated).
    Android Developer documentation

    But even integrated, it always need of kapt.
    So, add apply plugin: 'kotlin-kapt' at top of your build.gradle app file and remove the androidx.databinding:databinding-compiler dependency, should solves the problem. (It has worked for me)