Cate Daniel
Cate Daniel

Reputation: 744

Android databinding to a custom view

What I'm trying to do is pass a live data through to a custom view

What I'd like to do:

<com.mastherhealth.masterbilling.views.DashboardCard
    android:layout_height="@dimen/dashboard_card_size"
    android:layout_width="@dimen/dashboard_card_size"
    android:layout_marginTop="@dimen/dashboard_card_margin"
    android:layout_marginBottom="@dimen/dashboard_card_margin"
    android:layout_marginStart="@dimen/dashboard_card_center_margin"
    android:layout_marginEnd="@dimen/dashboard_card_center_margin"
    app:layout_constraintTop_toBottomOf="@+id/dashboard_doctor_spinner"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:cardCornerRadius="10dp"
    app:cardBackgroundColor="@color/colorPrimaryDark"
    app:name="Saved"
    app:number="@{`` + viewModel.savedClaimsLiveData}"/>

Where the viewModel.saveClaimsLiveData is a LiveData That I'm going to be getting from my repository.

my attrs.xml:

<resources>
    <declare-styleable name="DashboardCard">
        <attr name="name" format="string"/>
        <attr name="fontAwesome" format="string"/>
        <attr name="number" format="string"/>
    </declare-styleable>
</resources> 

I have a DashboardCard class that extends CardView, sets the name and number the way I'd like them to look.

I'm currently getting

****/ data binding error ****msg:Cannot find the setter for attribute 'app:number' with parameter type java.lang.String on com.mastherhealth.masterbilling.views.DashboardCard.

Upvotes: 1

Views: 794

Answers (1)

Jian
Jian

Reputation: 3258

You need to add a BindingAdapter for a custom attribute.

@BindingAdapter("number")
fun bindNumber(view: View, number: String) {
    // ...
}

Upvotes: 1

Related Questions