title: ViewDataBinding getVariable?
body: <p>The 2 main questions are :</p>
  
  <ul>
  <li>Why <a href="https://developer.android.com/reference/android/databinding/ViewDataBinding.html" rel="noreferrer">ViewDataBinding</a> doesn't have a method like <code>getVariable("variableName")</code> that will look up for a variable and returns it or null if no variable with this name exists.</li>
  <li>Is their any way/workaround to achieve this kind of behavior?</li>
  </ul>
  
  <p>So to be more explicit : if I don't know the type of my <a href="https://developer.android.com/reference/android/databinding/ViewDataBinding.html" rel="noreferrer">ViewDataBinding</a>, is their a way to get its variable or I must know its type?</p>
  
  <hr>
  
  <p><strong>Here is how it is working actually :</strong></p>
  
  <ol>
  <li>I havea layout called <strong>my_layout.xml</strong> :</li>
  </ol>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;layout&gt;
      &lt;data&gt;
          &lt;variable
              android:name="myVaribale"
              android:type="String"/&gt;
          &lt;/variable&gt;
      &lt;/data&gt;
  
      &lt;RelativeLayout
          android:id="@+id/root_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:text="@{myVariable}"/&gt;
  
      &lt;/RelativeLayout&gt;
  &lt;layout&gt;
  </code></pre>
  
  <ol start="2">
  <li><p>I inflate an instance of its <a href="https://developer.android.com/reference/android/databinding/ViewDataBinding.html" rel="noreferrer">ViewDataBinding</a> :</p>
  
  <p><pre><code>MyLayoutBinding binding = (MyLayoutBinding) DataBindingUtil.inflate(inflater, R.layout.my_layout, root, false);  </pre></code></p></li>
  <li><p>I can get its variable by calling the appropriate method :</p>
  
  <p><pre><code>String myVariable = binding.getMyVariable(); </pre></code></p></li>
  </ol>
  
  <p>So this was how to get a variable when <strong>we know the type</strong> of the <a href="https://developer.android.com/reference/android/databinding/ViewDataBinding.html" rel="noreferrer">ViewDataBinding</a>.</p>
  
  <hr>
  
  <p><strong>My problem is here :</strong></p>
  
  <p>Let's imagine I have 3 layouts called <code>my_layout_1</code>, <code>my_layout_2</code>, <code>my_layout_3</code> and those 3 layouts are surrounded by a <code>&lt;layout&gt;</code> tag.
  Those 3 layouts has also the same variable (<code>myVariable</code>).</p>
  
  <p>So here are the layouts :</p>
  
  <p><strong>my_layout_1</strong> 
  </p>
  
  <pre><code>&lt;layout&gt;
  
      &lt;data&gt;
          &lt;variable
              android:name="myVariable"
              android:type="String"/&gt;
          &lt;/variable&gt;
      &lt;/data&gt;
  
      &lt;RelativeLayout
          android:id="@+id/root_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:text="@{myVariable}"
              style="@style/my_style_1"/&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;layout&gt;
  </code></pre>
  
  <p><strong>my_layout_2</strong> 
  </p>
  
  <pre><code>&lt;layout&gt;
  
      &lt;data&gt;
          &lt;variable
              android:name="myVariable"
              android:type="String"/&gt;
          &lt;/variable&gt;
      &lt;/data&gt;
  
      &lt;RelativeLayout
          android:id="@+id/root_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:text="@{myVariable}"
              style="@style/my_style_2"/&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;layout&gt;
  </code></pre>
  
  <p><strong>my_layout_3</strong> 
  </p>
  
  <pre><code>&lt;layout&gt;
  
      &lt;data&gt;
          &lt;variable
              android:name="myVariable"
              android:type="String"/&gt;
          &lt;/variable&gt;
      &lt;/data&gt;
  
      &lt;RelativeLayout
          android:id="@+id/root_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:text="@{myVariable}"
              style="@style/my_style_3"/&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;layout&gt;
  </code></pre>
  
  <p>So the different layout represent a text with a different style (I know it can be achieved by others way but the goal is just to explain why I need this function <code>getVariable(variableName)</code>.</p>
  
  <p>Imagine now that I got a layout called <code>my_text_container</code> which will randomly contain one of the 3 layouts.</p>
  
  <p><strong>my_text_container</strong> 
  </p>
  
  <pre><code>&lt;layout&gt;
  
      &lt;FrameLayout
          android:id="@+id/container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/&gt;
  
  &lt;layout&gt;
  </code></pre>
  
  <p>So I'll add a text by doing something like this (again, it's just for demonstration purpose) :</p>
  
  <pre><code>MyTextContainerBinding textContainerBinding = (MyTextContainerBinding) DataBindingUtil.inflate(inflater, R.layout.my_text_container, root, false);
  int textLayoutRes;
  int randomNumber = generateNumberBetween0and2();
  switch(randomNumber) {
      case 0: textLayoutRes = R.layout.my_layout_1;
          break;
      case 1: textLayoutRes = R.layout.my_layout_2;
          break;
      case 2: textLayoutRes = R.layout.my_layout_3;
          break;
  }
  textContainerBinding.getRoot().addView(DataBindingUtil.inflate(inflater, textLayoutRes, root, false));
  </code></pre>
  
  <p>And finally I want to retrieve <code>myVariable</code> but I don't know if it's a layout 1, 2 or 3. Basically what could be done now is to use <a href="https://docs.oracle.com/cd/E13155_01/wlp/docs103/javadoc/com/bea/p13n/expression/operator/Instanceof.html" rel="noreferrer">instanceof</a> operator to check whether its a type <code>MyLayoutBinding1</code>, <code>MyLayoutBinding2</code> or <code>MylayoutBinding3</code>. But as I said this use-case is only for demonstration purpose and I could have 999 different layouts.</p>
  
  <p>So what I would like to do is :</p>
  
  <pre><code>ViewDataBinding myLayout = DataBindingUtil.getBinding(textContainerBinding.getRoot().getChildAt(0));
  String myVariable = myLayout.getVariable("myVariable");
  
  if(myVariable != null)
      Log.d(TAG, "myLayout was MylayoutBinding1 or myLayoutBinding2 or myLayoutBinding3");
  else
      Log.d(TAG, "mylayout was not one of the 3 layouts"); 
  </code></pre>
  
  <p>Sorry for this (very) long post but thank's for reading it and for any future answers !</p>