What is Android Jetpack ? 1.Architecture — DataBinding

Umit Kose
Huawei Developers
Published in
3 min readNov 13, 2020

What is this Jetpack that we see very often nowadays and we don’t care much enough :)

Jetpack consists of the library and tools recommended by Android. It enables developers to write higher quality, more efficient and faster running programs. It basically consists of 4 categories:

  • Architecture
  • Foundation
  • UI
  • Behavior

With Jetpack, developers can develop a project that works smoother on all android devices. It reduces boilerplate codes as well.

Architecture

The Architecture area of Jetpack includes eight different libraries and tools that help you architect your app. As below:

DataBinding
Lifecycles
LiveData
Navigation
Paging
Room
ViewModel
WorkManager

Foundation

The Foundation area of Jetpack involves core system components, Kotlin extensions and Testing Libraries.

  • Backward compatibility
  • Testing
  • Kotlin language support
  • App Compat
  • Android KTX
  • Multidex
  • Test

Behavior

Behavior part contains jetpack libraries that allow the user to interact with the UI.

  • Download Manager
  • Media & playback
  • Notifications
  • Permissions
  • Preferences
  • Sharing
  • Slices
  • Assistant

UI

Jetpack UI libraries include animations, fragments, palettes, layouts, Emojis, Android Auto, Wear and TV.

The diagram that I shared below can help you understand these components better:

What is DataBinding ?

Data binding lets us handle the communication between views and data sources reliably and simply. I mean that makes the communication between xml and code classes much easier. Lets see how it works :)

First we need to enable databinding module level gradle file in android tags.

dataBinding {
enabled = true
}

Most of the operations in DataBinding are done in XML files. It specifies which data model will be connected to this XML file.

Note: data tags always must define in layout tags

<data>

The data to be linked to this XML file must be specified between the <data> tags.

<variable>

It specifies which data to bind into the <variable> tag.

In this example, I assign a name to a car brand model that I have defined in Textview. As you can see:

android:text="@={brandname.name}"

So how we use it in Activity ?

ActivityMainBinding automatically created by Android Studio.

Whatever class you create, it will add “Binding” at the end of that.

At the 8. line, we must associate our layout xml file with DataBindingUtil. In this way, we can use databinding

How to add Click Listener ?

First we should define listener class and then create data tag again in xml file.

Similarly, we make the definitions in activity again.

Results

DataBinding might be very useful in large projects. The best feature I have seen is that it considerably reduces the crowd of code within the activities or fragments.

In my next articles, I will mention other features of Android Jetpack under the Architecture title.

Let’s meet again!

References

--

--