3

I have custom attribute which is declared as type of string, when a pass the value as just string in xml like this app:cpb_title="sometsring" its working but when i try data binding like this app:cpb_title"@{model.someStringField}" it gaves me error "cannot find setter for "app:cpb_title" that accepts parameter type java.lang.String"

how can i fix it?

attrs.xml

  <declare-styleable name="CircularProgressBar">
    <attr name="cpb_hasShadow" format="boolean"/>
    <attr name="cpb_progressColor" format="string"/>
    <attr name="cpb_backgroundColor" format="string"/>
    <attr name="cpb_title" format="string"/>
    <attr name="cpb_titleColor" format="string"/>
    <attr name="cpb_subtitle" format="string"/>
    <attr name="cpb_subtitleColor" format="string"/>
    <attr name="cpb_strokeWidth" format="integer"/>
</declare-styleable>

inside view class

 String t = a.getString(R.styleable.CircularProgressBar_cpb_title);
    if(t!=null)
        mTitle = t;

1 Answer 1

2

You can use BindingAdapter instead of declaring attributes in the attrs.xml file. Please do as below:

class BindingUtil {

    @BindingAdapter("cpb_title")
    public static void setTitle(TextView view, String title) {
        view.setText(title);
    }
}

Then, you can use app:cpb_title attribute in your XML as below:

               <Textview
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cpb_title="@{model.someStringField}" />
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for answer, where can i put this @BindingAdapter("cpb_title") ? inside the model class? or inside custom view class
you can place any class. Put in model class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.