Simplifying Android Development with DataBinding in Kotlin

Pranshu Gupta
3 min readMay 29, 2024

--

Hello everyone,

Welcome back to our ongoing series on Android development! We’ve covered some essential topics so far, including LiveData, ViewModel, and ViewModelFactory. Today, we turn our attention to another powerful feature that can significantly streamline your Android development process: DataBinding.

DataBinding in Android allows you to bind UI components in your layouts directly to data sources in your app using a declarative format rather than programmatically. This can lead to more readable, maintainable, and efficient code, reducing the boilerplate you often encounter in traditional Android development. (so basically hum databinding findViewById() ko replace karne keliye use karte hai taki hamara code concise ho, baar baar runtime pe views dhundhne nahi padte jisse app ki performance badh jati hai)

we use findViewById() to get references to view. everytime we use findViewById android system has to go through view hierarchy and find it at runtime it reduces the performance of large android application .

when we are using databinding we are creating a binding object that contains the reference of each view of a layout once a binding object has been created so android system doesn't have to go through the view hierarchy again and again.

Lets start implementing View Model Factory in our projects !!

you can practice with me by implementing it on this starter project just download it, extract it and open in your android studio(https://mega.nz/file/uroXAIjC#T6WbZhuyROH0bJbIGM9OxvtZ0TQBraeOkKK7CRrvEBg)
1. Enable databinding in app level gradle file

2. To create binding class for xml layout wrap the xml in layout tags and add the Namespace Declarations in the outermost layout .

so databinding library takes the xml layout activity_main that we wrapped in layout tag and create a binding object ActivityMainBinding (remember activity_main => ActivityMainBinding )

3. now declare a refernce variable for binding object

(The setContentView function is used to render our layout on the screen based on the value given by the user in the xml layout file views will be inflated and rendered but now with data binding we don’t need this function now we will use binding object for better efficiency)

4. Remove setContentView function and add code for databinding object creation code.

(our new databinding object is having properties for each of the views in the xml layout file properties name will be created by removing the underscores and using camel case example android:id=”@+id/name_edit_text” => nameEditText )

5. now we don’t need findViewById where ever you have use findViewById remove it and add binding.id ( If the id is greeting_text_view it will be converted to greetingTextView )

Now run the app. From now make all your apps faster and codes concise. 😉

--

--

Pranshu Gupta

hello! My name is pranshu gupta. i am an android development and opensource enthusiast. Additionally, I'm passionate about machine learning.