Learn simply about Databinding in Android

Tram Ho

# What is databinding? Data Binding is a library built into the Android Jetpack.
It allows linking logical data with UI Elements (eg: TextView, EditText, ImageView…). Different from using findByViewId: * Data binding creates binding object at compile time for all views in layout. * findViewByld will traverse the views at runtime when they are called. * Views accessed through the Binding object are more efficient than using findViewByld. * With complex structured layouts, using Databinding can significantly increase performance. ![image.png](https://images.viblo.asia/1472efa4-a299-4995-a94d-542b47c54789.png) You can see more Google official documents about Databinding [here](https ://developer.android.com/topic/libraries/data-binding/start). # The environment requires Data Binding to provide both flexibility and compatibility, but is only used for devices running Android 4.0 (API level 14) and above.
Comes with Android Plugin for Gradle version 1.5.0 or later, however it is recommended to use the latest version, [information about Android Plugin for Gradle versions](https://developer.android.com/studio/releases/ gradle-plugin#updating-plugin). # Basic usage 1. To use Databinding, first we need to config in the file gradle.build located in the app module Find the path app/build.gradle and add in the tag android{} as below: android { ... buildFeatures { dataBinding true } } 2. Next, add a pair of tags. wrap our entire layout file like below: 3. Declare and import binding class to be able to use 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.nameText.text = "Hi World!" } As above we can completely replace using traditional findByVIewId and instead use binding object to access the views in our layout at compile time. # Using Databinding with objects For example, we have a data class MyName and a layout with the structure as shown below: ![image.png](https://images.viblo.asia/50dfc3d6-29d5-4099-a02b -8980c7f559a7.png) To bind the layout views to the object’s data, we need to create a variable with the name “myName” along with the type being the package to the MainActivity file of that layout in the tag pair. inside .
Through the defined name we can access to get the object’s data according to the syntax @={myName.name} ![image.png](https://images.viblo.asia/9b2bf0c7-1bc1 -4876-a66c-b337e05c6e87.png) Finally in the MainActivity file we listen for the change of the object, when it changes, it will immediately be updated to the previously linked view. class MainActivity AppCompatActivity() { private lateinit var binding: ActivityMainBinding private val myName: MyName = MyName ( name: "Aleks") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingutil.setContentView ( activity:rthis, R. layout.activity_main) binding.nyName = myName } Similar to the image below: ![image.png](https://images.viblo.asia/f04524c8-7f94-4f97- a830-3ffe98365cdc.png) You can see more advanced samples [here.](https://github.com/android/databinding-samples)

Share the news now

Source : Viblo