title: Android - Make new view attribute and pass to its children in XML compound view component - data binding in layout
body: <h2>Full answer:</h2>
  
  <p><strong>1. Enable data binding in <em>app/build.gradle</em>:</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>dataBinding {
     enabled true
  }
  </code></pre>
  
  <p><strong>2. Use <em>DataBindingUtil</em> to set content view</strong></p>
  
  <p><strong>java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      DataBindingUtil.setContentView(this, R.layout.activity_engineering_mode_main);
  }
  </code></pre>
  
  <p><strong>3. Child item layout</strong></p>
  
  <p>You will see that I define 2 new attributes</p>
  
  <p><strong>values/bools.xml</strong></p>
  
  <pre><code>&lt;variable
      name="textTitle"
      type="String" /&gt;
  
  &lt;variable
      name="buttonVisibility"
      type="boolean" /&gt;
  </code></pre>
  
  <p>With <strong>textTitle</strong>, you can use any string from resource by <em>@string/string_name</em></p>
  
  <p>With <strong>buttonVisibility</strong>, you have to define <em>bool</em> resource type, and use <em>@bool/bool_name</em></p>
  
  <p><strong>layout/item_engineering_list_row.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;layout xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
  &lt;data&gt;
  
      &lt;import type="android.view.View" /&gt;
  
      &lt;variable
          name="textTitle"
          type="String" /&gt;
  
      &lt;variable
          name="buttonVisibility"
          type="boolean" /&gt;
  &lt;/data&gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="@dimen/engineer_actionbar_height"
      android:background="@color/engineer_background_color"
      android:orientation="horizontal"&gt;
  
      &lt;TextView
          android:id="@+id/engineer_txtName"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_marginLeft="@dimen/engineer_text_margin"
          android:layout_weight="1"
          android:gravity="center_vertical"
          android:text="@{textTitle}"
          android:textColor="@color/engineer_text_color"
          android:textSize="@dimen/engineer_title_font_size" /&gt;
  
      &lt;Button
          android:id="@+id/engineer_btnNext"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_marginRight="@dimen/engineer_text_margin"
          android:text="BACK"
          android:visibility="@{buttonVisibility ? View.VISIBLE : View.GONE, default=gone}" /&gt;
  
  &lt;/LinearLayout&gt;
  &lt;/layout&gt;
  </code></pre>
  
  <p><strong>4. Boolean resource file</strong></p>
  
  <p></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;bool name="item_button_visibility_default"&gt;false&lt;/bool&gt;
      &lt;bool name="item_button_visibility_on"&gt;true&lt;/bool&gt;
      &lt;bool name="item_button_visibility_off"&gt;false&lt;/bool&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p><strong>5. Parent layout, which includes some children and passes value to new attributes</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;layout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
  
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;include
          android:id="@+id/includedLayout0"
          layout="@layout/item_engineering_list_row"
          app:buttonVisibility="@{@bool/item_button_visibility_on}"
          app:textTitle="@{@string/app_name}" /&gt;
      &lt;include
          android:id="@+id/includedLayout1"
          layout="@layout/item_engineering_list_row"
          app:buttonVisibility="@{@bool/item_button_visibility_default}"
          app:textTitle="@{@string/app_name}" /&gt;
  &lt;/LinearLayout&gt;
  &lt;/layout&gt;
  </code></pre>
  
  <hr>
  
  <h2>Original Question:</h2>
  
  <p>I am new to android and I've been working with QML in QT for a time.</p>
  
  <p>I wonder how I can make a layout more easier by applying params in XML in compound view components.</p>
  
  <p>I have a custom layout item in xml and want to pass some attributes from a parent to its children, and I also want to initialize parent's attribute with new values to customize its children too.</p>
  
  <p>My concept is as below:</p>
  
  <p><strong>item.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      !!!! how to declare a new attribute here !!!
      | like this:
      | textTitle="New Title"      // default value for child
      | buttonVisibility="visible" // default value for child
      "&gt;
  
      &lt;TextView
          android:id="@+id/engineer_txtTitle"
          android:text= textTitle &lt;--- use parent's /&gt;
  
      &lt;Button
          android:id="@+id/engineer_btnBack"
          android:visibility= buttonVisibility  &lt;== use parent's /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>CLICK HERE TO SEE IMAGE: <a href="https://i.stack.imgur.com/rpmTN.jpg" rel="nofollow noreferrer">base Item</a></p>
  
  <p><strong>main.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;LinearLayout&gt;
      &lt;include
          android:id="@+id/item1"
          layout="@layout/item" 
          textTitle= "FIRST"
          // buttonVisibility not set here, use default as visible
          /&gt;
  
      &lt;include
          android:id="@+id/item2"
          layout="@layout/item"
          textTitle= "SECOND"
          buttonVisibility = "gone" // dont show button 
          /&gt;
   &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>CLICK HERE TO SEE IMAGE: <a href="https://i.stack.imgur.com/BQYO6.jpg" rel="nofollow noreferrer">apply with param</a></p>