Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot refer to other View ID in Android data binding

I just finished watching Advanced Data Binding - Google I/O 2016 and would like to apply the following to reduce repetition of my expression used in different views.

enter image description here

But I cannot make it work in my case:

<ImageButton                 android:id="@+id/btn_list"                 android:layout_width="48dp"                 android:layout_height="48dp"                 android:layout_gravity="start"                 android:background="@drawable/btn_s01_list"                 android:visibility="@{bean.shouldHideControls? View.GONE: View.VISIBLE}"/>              <ToggleButton                 android:id="@+id/btn_radar"                 android:layout_width="48dp"                 android:layout_height="48dp"                 android:background="@drawable/btn_radar_selector"                 android:checked="false"                 android:gravity="end"                 android:text=""                 android:textOff=""                 android:textOn=""                 android:visibility="@{btn_list.visibility}"/> 

and I got

Error:(426, 39) Identifiers must have user defined types from the XML file. btn_list is missing it

Edit:

I missed an important point in the same talk... The View IDs are camel-casified.

enter image description here

like image 436
chubao Avatar asked Jun 09 '16 13:06

chubao


People also ask

Can I use both data binding and view binding?

View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.

How do I enable data binding and view binding?

Step 2: Enabling the ViewBinding Feature There is a need to enabling the ViewBinding feature in Android Studio 4.0 and above, inside the app-level build gradle file. Invoke the following code snippet inside the android{} body of the gradle file.

What is the role of the ViewModel in the data binding?

ViewModel: It acts as a link between the Model and the View. It's responsible for transforming the data from the Model. It provides data streams to the View. It also uses hooks or callbacks to update the View.


1 Answers

The binding process converts your IDs to properties in the binding class, and the generated names are camel-casified.

You may need to change the following line:

android:visibility="@{btn_list.visibility}"/> 

To this:

android:visibility="@{btnList.visibility}"/> 
like image 184
earthw0rmjim Avatar answered Oct 02 '22 14:10

earthw0rmjim