Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android `@BindingAdapter` setter not being called

Here is my BindingAdapter:

public class Bindings{
    @BindingAdapter({"font"})
    public static void setFont(TextView textView, String fontName) {
        textView.setTypeface(FontCache.getInstance(textView.getContext()).get(fontName));
    }
}

*Instead of using "font" as the annotation parameter, I've tried "bind:font", "android:font", and "app:font", and made all the corresponding changes in the layout, but the BindingAdapter is still not called

Here is the layout where I use the BindingAdapter (bind_layout.xml):

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <data></data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            font="@{`fontawesome-webfront`}"
            android:text="@string/double_left"/>
    </LinearLayout>
</layout>

*this layout is included in the Activity's layout which is set using DatabindingUtils.setContentView

Here is the activity whose layout includes bind_layout.xml:

public class ACreateSemester extends AppCompatActivity {
    private List<CreateItemView> mCreateItemViews;
    private LinearLayout mItemContainer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DataBindingUtil.setContentView(this,R.layout.a_create_items);
        mItemContainer = (LinearLayout) findViewById(R.id.item_container);
        mItemContainer.addView(new CreateItemView(this, Item.getDefaultItem()));
    }

}

The three files I referenced here are listed in their entirety.

The way I know the BindingAdapter is not being called is because I set a breakpoint on the method and also in the body, and the breakpoint is never reached.

Any idea why the BindingAdapter is not firing?

like image 830
shoe Avatar asked Nov 05 '16 20:11

shoe


2 Answers

Please, also make sure that you have made this steps.

  1. Apply plugin: 'kotlin-kapt' in your app level build.gradle.
  2. Set lifecycle owner to binding from your activity's onCreate() method e.g. binding.lifecycleOwner = this, where this is your Activity.
  3. Change font="@{fontawesome-webfront}" to app:font="@{fontawesome-webfront}" in your xml.
  4. Add some Id to your button in xml layout.
like image 144
Mag Hakobyan Avatar answered Nov 05 '22 09:11

Mag Hakobyan


I think you missed to connect your view with viewModel in activity.

public class ACreateSemester extends AppCompatActivity {
private List<CreateItemView> mCreateItemViews;
private LinearLayout mItemContainer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ACreateItemsBinding binding =  DataBindingUtil.setContentView(this,R.layout.a_create_items);
    mItemContainer = (LinearLayout) findViewById(R.id.item_container);
    mItemContainer.addView(new  CreateItemView(this,Item.getDefaultItem()));
    binding.setView(YourViewHere);
}
like image 44
dara Avatar answered Nov 05 '22 09:11

dara