Monday, May 27, 2024
HomeAndroidAndroid Data Binding Using Kotlin

Android Data Binding Using Kotlin

-

Introduction:

Android Data binding is one of the hot topic among Android developers ever since its first launch in Mid 2015, helping to build the apps faster.

This Article explains about the same, Data binding Library concept and how to use it in android using Kotlin. Data Binding not only supports in eliminating boilerplate code but also helps in faster development times and faster execution times.

Advantages:

  • It eliminates boilerplate code (like findViewById)
  • It provides a stronger readability, it would be easy to learn about it.
  • Provides support to separate UI logic and business logic.

Similar Links: How to Read a File using Kotlin

Let’s see how to implement data binding in our application.

Configuring Android Data Binding:

To enable the usage of data binding in your app, first edit your application’s build.gradle file and add the following code snippet.


dataBinding
{
enabled = true
}

view raw

build.gradle

hosted with ❤ by GitHub

Data Binding Layout File:

Generally, in default layout files we have ViewGroup(E.g:LinearLayout, RelativeLayout) as parent tag. But, in this case to support data binding techniques, we must have <layout> tag as root tag. For a MainActivity class, a activity_main.xml layout would be like this:


<?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">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.processmap.databindingexample.MainActivity">
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="14dp"
android:text="Write Text Here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</layout>

Actual view (here it is Constraint layout) is defined within layout tag to support data binding techniques.The layout tag is a tag which indicates build system that, this file should be processed for data binding. Any layout file is without that tag , will not support data binding.

Data Binding Activity:

After this, binding class will be auto generated based on the name of layout file by default.To associate it in your class, invoke setContentView() using DataBindingUtil like this:


import android.databinding.DataBindingUtil
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.processmap.databindingexample.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private var mBinding: ActivityMainBinding? = null //This ActivityMainBinding class is auto generated by data binding library for us
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding=DataBindingUtil.setContentView(this,R.layout.activity_main) //Setting contentview usind data binding.
mBinding?.txtView?.setText("Hello!") //Accessing textview using mbinding
}
}

view raw

MainActivity.kt

hosted with ❤ by GitHub

That’s it, we are done. Run the project and check output.

References: Data Binding Library implementation

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

SOLID Principles in Android with Kotlin Examples

Recently I had an interview experience where I went blank when the interviewer posed an Interesting Question - Explain SOLID Principles and how they are...

Building a Multi Module App in Android | Modularization in Android #1

Recently, I was in requirement for developing a Multi Module app in Android and I was going through this great lecture on it. This article...

Lambda function in Kotlin with Examples

Lambda function is powerful feature in any Programming language and Lambda function in Kotlin is no exception. In this article, we will look at how...

Higher Order Functions in Kotlin with examples

There are many advanced features in Kotlin which gives an edge for the user using this language over Java. One such feature is Higher Order...

Follow us

1,358FansLike
10FollowersFollow
398SubscribersSubscribe

Most Popular