1. Please make yout .gradle setting like below.
inside android in build.gradle.kts
android{
dataBinding {
enable = true
}
}
2. Put below line inside gradle.properties
android.databinding.enableV2=true
3. add below dependencies build.gradle.kts(Module:app)
//coroutines
implementation(libs.androidx.coroutines.core.ktx)
implementation(libs.androidx.coroutines.ktx)
// Retrofit
implementation(libs.androidx.retrofit2.converter.gson)
implementation(libs.androidx.retrofit2)
implementation(libs.androidx.okhttp3.logging.interceptor)
//view model
implementation(libs.androidx.lifecycle.livedata.ktx)
implementation(libs.androidx.lifecycle.viewmodel.ktx)
//New Material Design
implementation(libs.androidx.android.material.ktx)
implementation(libs.androidx.arch.lifecycle.viewmodel)
4. Add below inside libs.versions.toml
[versions]
coroutinesVersion = "1.7.1"
retrofit = "2.9.0"
gson = "2.6.2"
okhttp3 = "4.5.0"
lifecycle_version = "2.8.4"
material_version = "1.12.0"
androidx-coroutines-core-ktx = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutinesVersion" }
androidx-coroutines-ktx = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "coroutinesVersion" }
androidx-retrofit2 = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" }
androidx-retrofit2-converter-gson = { group = "com.squareup.retrofit2", name = "converter-gson", version.ref = "gson" }
androidx-okhttp3-logging-interceptor = { group = "com.squareup.okhttp3", name = "logging-interceptor", version.ref = "okhttp3" }
androidx-lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycle_version" }
androidx-lifecycle-livedata-ktx = { group = "androidx.lifecycle", name = "lifecycle-livedata-ktx", version.ref = "lifecycle_version" }
androidx-android-material-ktx={group="com.google.android.material",name="material",version.ref="material_version"}
androidx-arch-lifecycle-viewmodel={group="android.arch.lifecycle",name="viewmodel",version="1.1.1"}5. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="com.example.basicmvvmdatabinding.viewModel.MainActivityViewModel" />
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".view.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editTextTextEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="30dp"
android:ems="10"
android:text="@={viewModel.email}"
android:hint="Enter Email"
android:inputType="textEmailAddress"
android:padding="20dp"
android:singleLine="true" />
<EditText
android:id="@+id/editTextNumberPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextTextEmailAddress"
android:layout_centerInParent="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:ems="10"
android:text="@={viewModel.password}"
android:hint="Enter Name"
android:inputType="numberPassword"
android:padding="20dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextNumberPassword"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:onClick="@{()->viewModel.onLoginBtnClick()}"
android:text="Login" />
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>6.MainActivity.kt
package com.example.basicmvvmdatabinding.view
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.example.basicmvvmdatabinding.R
import com.example.basicmvvmdatabinding.databinding.ActivityMainBinding
import com.example.basicmvvmdatabinding.viewModel.LoginListener
import com.example.basicmvvmdatabinding.viewModel.MainActivityViewModel
class MainActivity : AppCompatActivity(), LoginListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val dataBinding: ActivityMainBinding =
DataBindingUtil.setContentView(this, R.layout.activity_main)
val viewModel1 = ViewModelProvider(this)[(MainActivityViewModel::class.java)]
dataBinding.viewModel = viewModel1
viewModel1.listener = this
}
override fun started() {
Toast.makeText(this, "Started", Toast.LENGTH_SHORT).show()
}
override fun success() {
Toast.makeText(this, "Success", Toast.LENGTH_LONG).show()
}
override fun failed(message: String?) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
}7.MainActivityViewModel.kt
package com.example.basicmvvmdatabinding.viewModel
import androidx.lifecycle.ViewModel
class MainActivityViewModel : ViewModel() {
var email: String? = null
var password: String? = null
var listener: LoginListener? = null
fun onLoginBtnClick() {
listener?.started()
if (email.isNullOrEmpty() || password.isNullOrEmpty()) {
//show error message
listener?.failed("Invalid Email or Password")
return
}
//success message
listener?.success()
}
}8.LoginListener.kt
package com.example.basicmvvmdatabinding.viewModel
interface LoginListener {
fun started()
fun success()
fun failed(message: String?)
}
No comments:
Post a Comment