Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Boolean to include tag in Android Data Binding

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?

like image 494
Khemraj Sharma Avatar asked Jul 17 '18 11:07

Khemraj Sharma


People also ask

How to pass a value to included layout in data binding?

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.

How do I bind to an include in Android?

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:

How does data binding work for attributes in Android?

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.

How to enable databinding in Android with Gradle?

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.


1 Answers

You need to pass boolean as: app:menuVisible="@{true}".

like image 139
Krystian Avatar answered Nov 09 '22 23:11

Krystian