title: LiveData two-way databinding WITHOUT exposing MutableLiveData
body: <p>I'm trying to use two-way databinding on a EditText, that works fine if I expose the field as MutableLiveData as it is usually seen on examples I found online.</p>
  
  <p>However there are good reasons not to expose MutableLiveData and those reasons aren't magically invalid because I decided to use the databinding library.  </p>
  
  <h2>EDIT: The main motivation here is MyViewModel should remain in control of setting data (this is the reason why it is not recommended to expose <code>MutableLiveData</code> directly), in the setter I can perform whatever checks or transformations necessary and then just call <code>setValue</code> on the LiveData.</h2>
  
  <p>I usually expose a <code>LiveData</code> getter and a separate setter from my ViewModel, I tried to get this working with two-way data binding by using the <code>InverseMethod()</code> annotation, but that won't really work because databinding is looking for a InverseMethod to <code>getValue()</code> of the LiveData itself.</p>
  
  <p>Here is a simple example:</p>
  
  <pre><code>public class MyViewModel extends ViewModel {
  
      private MutableLiveData&lt;String&gt; mEmail = new MutableLiveData&lt;&gt;();
  
      // @InverseMethod("setEmail")    ### THIS DOESN'T WORK
      public LiveData&lt;String&gt; getEmail() {
          return mEmail;
      }
  
      // ### I WANT DATA-BINDING TO USE THIS METHOD
      public void setEmail(String email) {
          if (mEmail.getValue() != email) {
              mEmail.setValue(email);
          }
      }
  }
  </code></pre>
  
  <p>and this how a want to bind it</p>
  
  <pre><code>&lt;EditText
     android:id="@+id/input_email"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@={viewmodel.email}"/&gt;
  </code></pre>
  
  <p>the only workaround so far that works is using one-way data-binding to set the text on the EditText and then attach a TextWatcher and call my ViewModel.setter from there.</p>
  
  <p>EDIT:<br>
  second workaround is to extend MutableLiveData and then do the checks and transformations in an overridden <code>setValue</code> ... that's a lot of boilerplate to write.</p>