VladMorzhanov
9/23/2017 - 3:02 PM

Android xml view example for mvvm pattern with data binding.

Android xml view example for mvvm pattern with data binding.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="ViewModel"
            type="com.dev.ui.auth.ViewModel"/>
    </data>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/etAuthName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="150dp"
        android:ems="10"
        android:hint="Name"
        android:inputType="textPersonName"
        android:text="@={ViewModel.name}"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/etAuthEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Email"
        android:text="@={ViewModel.email}"
        android:inputType="textEmailAddress"
        app:layout_constraintEnd_toEndOf="@+id/etAuthName"
        app:layout_constraintStart_toStartOf="@+id/etAuthName"
        app:layout_constraintTop_toBottomOf="@+id/etAuthName" />

    <EditText
        android:id="@+id/etAuthPwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Password"
        android:text="@={ViewModel.pwd}"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="@+id/etAuthEmail"
        app:layout_constraintStart_toStartOf="@+id/etAuthEmail"
        app:layout_constraintTop_toBottomOf="@+id/etAuthEmail" />

    <TextView
        android:id="@+id/tvAuthForgotPwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Forgot password?"
        android:onClick="@{() -> ViewModel.onClickedForgotPwd()}"
        android:textColor="@android:color/holo_blue_light"
        app:layout_constraintEnd_toEndOf="@+id/etAuthPwd"
        app:layout_constraintStart_toStartOf="@+id/etAuthPwd"
        app:layout_constraintTop_toBottomOf="@+id/etAuthPwd" />

    <Button
        android:id="@+id/btnAuthAuth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Auth"
        android:onClick="@{() -> ViewModel.onClickedAuth()}"
        tools:android="onClick"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btnAuthGoogle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:onClick="@{() -> ViewModel.onClickedAuthGoogle()}"
        android:text="@string/common_signin_button_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/btnAuthFacebook"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:onClick="@{() -> ViewModel.onClickedAuthFacebook()}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>


</android.support.constraint.ConstraintLayout>
</layout>