Passing string from DataBinding in to custom view xml attribute

974 Views Asked by At

I have situation where want to pass string in to custom view attribute, but it not working if not passing only a reference.

Working:

......
app:title="sa"
app:title="@string/winning_title"
......

Not working:

  <data>

        <variable
            name="title"
            type="String" />
    </data>
......

   app:title="@{title}"

And stylable:

   <declare-styleable name="CollapsibleRecyclerView">
        <attr name="title" format="string"/>
        <attr name="separatorsColor" format="color"/>
        <attr name="animationDuration" format="integer"/>
    </declare-styleable>

Any ideas how to fix it?

1

There are 1 best solutions below

0
limdale On

Since you used "@{...}", I believe Android will automatically attempt to use data binding on it and look for a corresponding custom BindingAdapter.

You could define something like this and see if it works:

@BindingAdapter("app:title")
public static void setPaddingLeft(YourView view, String text) {
  view.doSomething(text);
}