DataBinding in Android
Overview
Android DataBinding is a way to attach user interface with business logic. DataBinding eliminates the way of manual intervention. If there is a change in data, user interface will update automatically. This reduces a lot of boilerplate codes.
Steps to follow
- Enable databinding in build.gradle (Module) in the android section
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
}
buildFeatures {
dataBinding true
}
/*or*/
dataBinding {
enabled true
}
- Import the dependency in build.gradle (Module) in the dependencies section
kapt "com.android.databinding:compiler:3.1.4"
- Enable DataBinding in layout.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="user"
type="info.androidhive.databinding.User" />
</data>
<LinearLayout 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:orientation="vertical"
android:padding="@dimen/fab_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.email}" />
</LinearLayout>
</layout>
- Create an instance of DataBinding in MainActivity with the help of DataBindingUtil.setContentView() method
class MainActivity : AppCompatActivity()
{
private var user : User? = null
override fun onCreate(savedInstanceState : Bundle?)
{
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main);
val binding : ActivityMainBinding =
DataBindingUtil.setContentView(this , R.layout.activity_main)
user = User("Ravi Tamada", "ravi@androidhive.info")
binding.setUser(user)
}
}
Various ways of DataBinding
DataBinding in Activity
class MainActivity : AppCompatActivity()
{
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState : Bundle?)
{
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this , R.layout.activity_main)
binding.tvLabel.text = "Introduction of Data Binding"
}
}
Declarative Binding in xml file and Activity
class MainActivity : AppCompatActivity()
{
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState : Bundle?)
{
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this , R.layout.activity_main)
binding.quote = Quote("Hello World!!!", "Raaj Kanchan")
}
}
<?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="quote"
type="com.example.databinding_android.Quote" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvQuote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{quote.quote}"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@{quote.author}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvQuote" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
One Way DataBinding with ViewModel
class MainActivity : AppCompatActivity()
{
private lateinit var binding : ActivityMainBinding
private lateinit var mainViewModel : MainViewModel
override fun onCreate(savedInstanceState : Bundle?)
{
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this , R.layout.activity_main)
mainViewModel = ViewModelProvider(this)[MainViewModel::class.java]
binding.mainViewModel = mainViewModel
binding.lifecycleOwner = this
}
}
data class Quote(val quote : String , val author : String)
class MainViewModel : ViewModel()
{
private val quotesObject = MutableLiveData(Quote("Life of Stray!" , "Raj Kanchan"))
val quotesLive : LiveData<Quote>
get() = quotesObject
private val releasingObject = MutableLiveData("24th December 2002")
val releasing : LiveData<String>
get() = releasingObject
fun getQuotes()
{
quotesObject.value = Quote("Harry Potter and the Goblet of Fire" , "J.K. Rowling")
}
fun getReleasingData()
{
releasingObject.value = "25th December 2002"
}
}
<?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="mainViewModel"
type="com.example.databinding_android.MainViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--<EditText
android:id="@+id/edtReleasingInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="@={mainViewModel.releasing}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvReleasingInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@{mainViewModel.releasing}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtReleasingInfo" />-->
<TextView
android:id="@+id/tvQuote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{mainViewModel.quotesLive.quote}"
app:layout_constraintBottom_toTopOf="@+id/tvAuthor"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{mainViewModel.quotesLive.author}"
app:layout_constraintBottom_toTopOf="@id/btnUpdate"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvQuote" />
<Button
android:id="@+id/btnUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> mainViewModel.getQuotes()}"
android:text="Update"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvAuthor" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
References
- https://www.vogella.com/tutorials/AndroidDatabinding/article.html
- https://www.androidhive.info/android-working-with-databinding/
Comments
Post a Comment