As it supports Data Binding menu in android? I write this code, but error: "Error:(16, 26) No resource type specified (at 'visible' with value '@{item.visible}')."
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="item"
type="ru.dixy.ubiworkerchecklistsmobile.Models.Fact"/>
<import type="android.view.View"/>
</data>
<item
android:id="@+id/compliteitem"
android:title="mybutton"
android:icon="@drawable/complite"
android:visible="@{item.visible}"
app:showAsAction="ifRoom"
/>
</menu>
AFAIK, at the moment, data binding is only for layout resources, not menu resources.
"At the moment, data binding is only for layout resources, not menu resources"
But, the behaviour can be achieved with Observable.OnPropertyChangedCallback. First you need to define OnPropertyChangedCallback:
Assuming that you have a binding for Fact model in your fragment:
Now you need to register propertyChangedCallback and unregister it when you are done with it:
Now we are ready update your view state based on the Fact model:
I realize this is an old question, but I wanted to provide a solution so it can help others with the same problem. This can be achieved using an action view for the menu item. It requires a fair bit of code, but it's an approach that uses MVVM and can be used for any data binding.
This is an example where the icon shows a count and changes the background if the count is greater than 0.
Define the menu item
menu/main.xml
Define the view model for the menu item.
Define a callback that will be implemented by the activity when the menu item is clicked.
Create a layout for the action view. The layout uses the view model class and set the text for the count and background. The callback interface is used for the OnClickListener for the action view.
layout/menu_action_count.xml
Note that a custom binding adapter is used for the
android:src
attribute. This is a good adapter for setting ImageView src via data binding.Lastly, inflate the menu and bind the action view layout in the activity.
For completeness, here are all the files in the sample that are not defined above.
drawable/ic_menu_blue_square.xml
drawable/ic_menu_red_square.xml
layout/activity_main.xml
MainActivityActionCallback.java
MainActivity.java