title: How to set click listener and pass edittext fields value to view model using data binding
body: <p>Hi I am trying to use data binding and mvvm architecture in my android app. I want to add click listener using data binding in the layout and send the values of username and password <code>edittext</code> to the view model and it will execute the web service and call appropriate method of <code>LoginActivity</code> like <code>startHomeActivity()</code>.</p>
  
  <p>Does anyone know how to do this or Am I taking wrong approach ? I have below snippet of code of my activity, layout and view model</p>
  
  <p>LoginActivity.kt</p>
  
  <pre><code>class LoginActivity : BaseActivity(), LoginNavigator {
  
      @Inject
      lateinit var loginViewModel: LoginActivityViewModel
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
  
          val activityLoginBinding = DataBindingUtil.setContentView&lt;ActivityLoginBinding&gt;(this, R.layout.activity_login)
  
  
      }
  
      override fun startHomeActivity() {
          TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
      }
  
      override fun startRegistrationActivity() {
          TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
      }
  
      override fun startForgotPasswordActivity() {
          TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
      }
  
      override fun handleError(throwable: Throwable) {
          TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
      }
  
  }
  </code></pre>
  
  <p>LoginActivityViewModel.kt</p>
  
  <pre><code>class LoginActivityViewModel {
  
  
      fun login(email: String, password: String) {
  
      }
  
      /**
       * Validate email and password. It checks email and password is empty or not
       * and validate email address is correct or not
       * @param email email address for login
       * @param password password for login
       * @return true if email and password pass all conditions else false
       */
      fun isEmailAndPasswordValid(email: String, password: String): Boolean {
  
          if (email.isEmpty()) return false
  
          if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) return false
  
          if (password.isEmpty()) return false
  
          return true
      }
  
  }
  </code></pre>
  
  <p>activity_login.xml</p>
  
  <pre><code>&lt;layout&gt;
  
      &lt;ScrollView 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"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fillViewport="true"
          tools:context="com.app.android.login.LoginActivity"
          tools:ignore="missingPrefix"&gt;
  
          &lt;android.support.constraint.ConstraintLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:paddingBottom="@dimen/default_view_margin_bottom_8dp"&gt;
  
              &lt;android.support.design.widget.TextInputLayout
                  android:id="@+id/til_login_email"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginEnd="@dimen/default_view_margin_right_8dp"
                  android:layout_marginStart="@dimen/default_view_margin_left_8dp"
                  android:textColorHint="@color/colorSecondaryText"
                  app:hintTextAppearance="@style/AppTheme.InputLayoutStyle"
                  app:layout_constraintBottom_toTopOf="@+id/til_login_password"
                  app:layout_constraintTop_toTopOf="parent"
                  app:layout_constraintVertical_chainStyle="packed"&gt;
  
                  &lt;android.support.design.widget.TextInputEditText
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:hint="@string/login_email"
                      android:imeOptions="actionNext"
                      android:singleLine="true"
                      android:textColor="@color/colorPrimaryText" /&gt;
              &lt;/android.support.design.widget.TextInputLayout&gt;
  
              &lt;android.support.design.widget.TextInputLayout
                  android:id="@+id/til_login_password"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginEnd="@dimen/default_view_margin_right_8dp"
                  android:layout_marginStart="@dimen/default_view_margin_left_8dp"
                  android:textColorHint="@color/colorSecondaryText"
                  app:hintTextAppearance="@style/AppTheme.InputLayoutStyle"
                  app:layout_constraintBottom_toTopOf="@+id/btn_login_login"
                  app:layout_constraintTop_toBottomOf="@+id/til_login_email"
                  app:layout_constraintVertical_chainStyle="packed"&gt;
  
                  &lt;android.support.design.widget.TextInputEditText
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:hint="@string/login_password"
                      android:imeOptions="actionDone"
                      android:singleLine="true"
                      android:textColor="@color/colorPrimaryText" /&gt;
              &lt;/android.support.design.widget.TextInputLayout&gt;
  
              &lt;Button
                  android:id="@+id/btn_login_login"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginEnd="@dimen/default_view_margin_right_8dp"
                  android:layout_marginStart="@dimen/default_view_margin_left_8dp"
                  android:layout_marginTop="48dp"
                  android:text="@string/login_btn_text"
                  android:textColor="@color/colorWhite"
                  app:layout_constraintBottom_toTopOf="@+id/textview_login_forgot_password"
                  app:layout_constraintTop_toBottomOf="@+id/til_login_password"
                  app:layout_constraintVertical_chainStyle="packed" /&gt;
  
              &lt;TextView
                  android:id="@+id/textview_login_forgot_password"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginEnd="@dimen/default_view_margin_right_8dp"
                  android:layout_marginStart="@dimen/default_view_margin_left_8dp"
                  android:layout_marginTop="36dp"
                  android:gravity="center"
                  android:text="@string/login_forgot_password"
                  app:layout_constraintBottom_toTopOf="@+id/btn_login_register"
                  app:layout_constraintTop_toBottomOf="@+id/btn_login_login"
                  app:layout_constraintVertical_chainStyle="packed" /&gt;
  
              &lt;Button
                  android:id="@+id/btn_login_register"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginEnd="@dimen/default_view_margin_right_8dp"
                  android:layout_marginStart="@dimen/default_view_margin_left_8dp"
                  android:text="@string/login_sign_up"
                  android:textColor="@color/colorWhite"
                  app:layout_constraintBottom_toBottomOf="parent" /&gt;
  
          &lt;/android.support.constraint.ConstraintLayout&gt;
      &lt;/ScrollView&gt;
  &lt;/layout&gt;
  </code></pre>