This is mistake. We have not tried data binding tags, mainly because the tags are special.
When using pre-ICS device targeting, Android data binding takes on the tag of the outermost layout element. This tag is mainly used for lifecycle bindings and is used by DataBindingUtil.findBinding() and DataBindingUtil.getBinding() .
So, since data binding does not work with tags, the only workaround is to not provide a tag for your LinearLayout or supply a fixed tag or resource string. If you are targeting ICS or later, you can reassign the tag after the layout is bound:
MenuItemBinding binding = MenuItemBinding.inflate(layoutInflater); binding.getRoot().setTag(menuItem);
You can also create a BindingAdapter for the new attribute:
@BindingAdapter("specialTag") public static void setSpecialTag(View view, Object value) { view.setTag(value); }
and then use it in your layout:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" app:specialTag="@{menuItem}" tools:ignore="UseCompoundDrawables"/>
This will allow you to use findViewByTag() and all the other things you expect.
However, this will not work if you aim at Honeycomb devices and earlier. This is not enough. You may be tempted to do something like this:
@BindingAdapter("specialTag") public static void setSpecialTag(View view, Object value) { view.setTag(R.id.app_tag, value); }
You cannot use findViewByTag with this approach, but it will save any value you want when using your view. But the reason we donβt use ID'd tags with Honeycomb before is because there is a memory leak when using ID'd tags, so donβt do this.
Hope this helps. I will log an error internally to support data-bound android: tags.