title: Android Kotlin Calling ViewModel function from View with Parameters
body: <p>I am building an Android app.</p>
  
  <p>I have a layout that contains a button 'saveButton'. When the user clicks the button, the onClick should call a function in my ViewModel, onSave(). This function requires 2 parameters: the text contents of 2 EditText views that are also present in the same layout.</p>
  
  <p>Basically, the user has edited the name and/or the synopsis and now wants to have the ViewModel update the object's data in the database.</p>
  
  <p>(Part of) my UI (.xml fragment layout):</p>
  
  <pre><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"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context=".ui.CreateEditRelationFragment"&gt;
  
  &lt;data&gt;
      &lt;variable
          name="createEditRelationViewModel"
          type="be.pjvandamme.farfiled.viewmodels.CreateEditRelationViewModel" /&gt;
  &lt;/data&gt;
  
  &lt;androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;Button
          android:id="@+id/saveButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/saveText"
          android:onClick="@{() -&gt; createEditRelationViewModel.onSave(relationNameEditText.getEditText().getText().toString(), relationSynopsisEditText.getEditText().getText().toString())}" /&gt;
  
      &lt;Button
          android:id="@+id/cancelButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="32dp"
          android:text="@string/cancelText"
          android:onClick="@{() -&gt; createEditRelationViewModel.onCancel()}" /&gt;
  
      &lt;EditText
          android:id="@+id/relationSynopsisEditText"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginTop="8dp"
          android:ems="10"
          android:gravity="start|top"
          android:inputType="textMultiLine" /&gt;
  
      &lt;EditText
          android:id="@+id/relationNameEditText"
          android:layout_width="@dimen/relationNameEditWidth"
          android:layout_height="wrap_content"
          android:layout_marginTop="8dp"
          android:layout_marginBottom="16dp"
          android:ems="10"
          android:inputType="textPersonName"
          android:text="Name" /&gt;
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>(Part of) the ViewModel:</p>
  
  <pre><code>class CreateEditRelationViewModel (
      private val relationKey: Long?,
      val database: RelationDao,
      application: Application
  ): AndroidViewModel(application){
  private var viewModelJob = Job()
  
  private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
  private var relation = MutableLiveData&lt;Relation?&gt;()
  
  private var _navigateToRelationDetail = MutableLiveData&lt;Boolean&gt;()
  val navigateToRelationDetail: LiveData&lt;Boolean&gt;
      get() = _navigateToRelationDetail
  
  fun onSave(name: String, synopsis: String){
      Timber.i("Got name: " + name + " and synopsis: " + synopsis)
      if(relationKey == null){
          uiScope.launch{
              relation.value = Relation(0, name, synopsis, false)
              insert(relation.value!!)
          }
      }
      else{
          uiScope.launch{
              relation.value?.name = name
              relation.value?.synopsis = synopsis
              update(relation.value)
          }
      }
      _navigateToRelationDetail.value = true
  }
  
  private suspend fun insert(newRelation: Relation){
      withContext(Dispatchers.IO){
          database.insert(newRelation)
      }
  }
  
  private suspend fun update(relation: Relation?){
      if(relation != null) {
          withContext(Dispatchers.IO) {
              database.update(relation)
          }
      }
  }
  }
  </code></pre>
  
  <p>I would want this thing to compile so that the onSave() function is called and the contents of the EditTexts passed as parameters.</p>
  
  <p>I cannot manage to pass the text contents of these EditTexts. The compiler throws this error:</p>
  
  <pre><code>[databinding] {"msg":"cannot find method getEditText() in class android.widget.EditText","file":"D:\\ etc.
  </code></pre>
  
  <p>It does the same thing when I try to access using the .text property directly.
  Does anyone know what I'm doing wrong? I'm tearing my hair out.</p>