android kotlin - ViewModel LiveData DataBinding example

MainActivity.kt

package com.cfsuman.jetpack

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import kotlinx.android.synthetic.main.activity_main.*
import androidx.lifecycle.ViewModelProviders
import com.cfsuman.jetpack.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var mModel: RandomUUIDViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Obtain the view model component
        mModel = ViewModelProviders.of(this).get(RandomUUIDViewModel::class.java)

        // Inflate view and obtain an instance of the binding class
        // ActivityMainBinding came from activity_main.xml with Binding suffix
        // Make your data type as this format
        val binding:ActivityMainBinding = DataBindingUtil.setContentView(
            this, R.layout.activity_main) as ActivityMainBinding

        // Set the view model
        binding.randomUUIDViewModel = mModel

        // Set the life cycle owner
        binding.setLifecycleOwner(this)

        // Set the initial uuid
        mModel.setRandomUUID()

        // Button click listener
        button.setOnClickListener{
            // Set the data
            mModel.setRandomUUID()
        }
    }
}
RandomUUIDViewModel.kt

package com.cfsuman.jetpack

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import java.util.*


class RandomUUIDViewModel: ViewModel(){

    private val randomUUID = MutableLiveData<String>()

    fun setRandomUUID(){
        randomUUID.value = UUID.randomUUID().toString()
    }

    fun getRandomUUID(): LiveData<String>{
        return randomUUID
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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">
    <data>
        <variable 
                name="randomUUIDViewModel" 
                type="com.cfsuman.jetpack.RandomUUIDViewModel"/>
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/rootLayout"
            tools:context=".MainActivity"
            android:background="#fdfdfc">
        <Button
                android:text="Generate Random UUID"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginStart="8dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintHorizontal_bias="0.454"
                android:layout_marginTop="32dp"
                app:layout_constraintTop_toTopOf="parent"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:id="@+id/textView"
                android:text="@{randomUUIDViewModel.randomUUID}"
                android:layout_marginTop="32dp"
                app:layout_constraintTop_toBottomOf="@+id/button"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginStart="8dp"
                app:layout_constraintStart_toStartOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
gradle dependencies

def lifecycle_version = "2.0.0"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
gradle settings

android {
    dataBinding {
        enabled = true
    }
}
gradle.properties

# To enable the new data binding compiler
android.databinding.enableV2=true