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?
Please, also make sure that you have made this steps.
fontawesome-webfront
}" to app:font="@{fontawesome-webfront
}"
in your xml.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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With