I am making common layout with data binding. I am having problem with passing boolean value to include tag.
I want achieve something like below
<include
layout="@layout/layout_toolbar"
app:menuVisible="true"
/>
Here is layout_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View"/>
<variable
name="menuVisible"
type="Boolean"/>
</data>
<ImageView
android:visibility="@{menuVisible ? View.VISIBLE : View.GONE, default=gone}"
/>
</layout>
What is appropriate way to do this?
It adds "Data binding doesn't support include as a direct child of a merge element" Just set id to included layout, and use binding.includedLayout.anyView. This example helps passing a value to <include & accessing included views in code. You have layout_common.xml, want to pass String to included layout.
You can make your bind work on your include just adding a ID to it like so: <include android:id="@+id/loading" layout="@layout/loading_layout" bind:booleanVisibility="@ {viewModel.showLoading}" /> An other interesting thing on this is that you can pas variables to the imported layout from the binder like this:
For an attribute, data binding tries to find the method setAttribute. The namespace for the attribute does not matter, only the attribute name itself. For example, an expression associated with TextView's attribute android:text will look for a setText (String). If the expression returns an int, data binding will search for a setText (int) method.
Enable DataBinding Navigate to Gradle Scripts > gradle.scripts (module) and add the following code to it. Step 3. Working on XML files Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
You need to pass boolean as: app:menuVisible="@{true}"
.
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