Getting values from other Views in Android Data Binding

Photo by Henri L. on Unsplash

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
  <data>
    <variable
        name="viewModel"
        type="example.MyViewModel"/>
  </data>

  <android.support.constraint.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:padding="16dp"
      >
    <android.support.design.widget.TextInputEditText
        android:id="@+id/edit_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />

    <Button
        android:id="@+id/button_ok"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:onClick="@{(v) -> viewModel.action(editName.getText().toString())}"
        app:layout_constraintTop_toBottomOf="@+id/edit_name"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />
  </android.support.constraint.ConstraintLayout>
</layout>

ポイント

  • Java or Kotlin 側で使うように CamelCase で view の ID を書く