title: Android Data Binding Programmatically Instantiated View
body: <p>The Android documentation does a great job of describing how one can create a binding class using a layout xml file. But I have a couple of questions.</p>
  
  <p>Is there a way to create a data binding class for a custom view that is instantiated programmatically? For example, lets say I have two custom view classes and I want to bind the same view model object to them programmatically without using any xml. The classes are as follows:</p>
  
  <pre><code>class MyViewModel {
  }
  
  class MyCustomView extends View {
  }
  
  class MyAnotherCustomView extends MyCustomView {
  }
  </code></pre>
  
  <p>Now lets say I instantiate MyCustomView/MyAnotherCustomView using:</p>
  
  <pre><code>MyCustomView customView = new MyCustomView(context);
  </code></pre>
  
  <p>How do I go about using data binding in this case? Is this possible using the official Android data binding framework? If not, what other frameworks/libraries are available or recommended to achieve this?</p>
  
  <p>My second question is a follow up of the first question. Lets say it is not possible to achieve what I want in my first question. Then, I will have to define a <code>my_custom_view.xml</code> file. This will look something like this:</p>
  
  <pre><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;variable name="user" type="com.example.User"/&gt;
     &lt;/data&gt;
     &lt;com.example.name.MyCustomView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:text="@{user.firstName}"/&gt;
  &lt;/layout&gt;
  </code></pre>
  
  <p>Now, if I want to use MyAnotherCustomView which is a subclass of MyCustomView keeping the binding logic the same, will I have to create a new xml file <code>my_another_custom_view.xml</code> just to replace MyCustomView with MyAnotherCustomView to define the same binding?</p>