Skip to content
tezvyn:

Data Binding: Link Android UI to Data in XML

Source: developer.android.comHardHow cards are made

Data Binding: Link Android UI to Data in XML

Data Binding lets you connect UI components to data sources directly in XML, eliminating manual findViewById calls. It's used to set view properties based on your ViewModel's state.

Why it exists

To reduce boilerplate code in activities and fragments. Manually finding each view by its ID and then setting its properties is repetitive, error-prone with potential null pointers, and clutters the UI logic. Data Binding was created to solve this by moving the connection between data and views into the layout file itself.

The mental model

Think of your XML layout not as a static blueprint, but as a template that can be filled with data. Instead of programmatically pushing data into views from your code (e.g., textView.setText(user.name)), you declare in the XML that a TextView's text is the user's name (e.g., android:text="@{user.name}"). The library handles the wiring.

How it works

After enabling the library, you wrap your XML layout in a <layout> tag. Inside, a <data> block declares variables, like a ViewModel. You then use the @{...} expression syntax to bind view attributes to the properties of these variables. At compile time, the library generates a binding class (e.g., ActivityMainBinding) that provides type-safe access to both the views and the data variables, connecting everything.

When to use it

Use Data Binding when you need more than just view references. It excels at binding UI to observable data sources, where the UI updates automatically when the data changes. It's also powerful for two-way data binding (e.g., user input in an EditText directly updates a ViewModel property) and using custom binding adapters for complex logic in XML.

When not to use it

If your only goal is to eliminate findViewById() calls, Data Binding is overkill. The official recommendation is to use the simpler, more lightweight, and better-performing View Binding library for that purpose. View Binding provides type-safe access to views without the added complexity and build-time cost of Data Binding's expression language.

One canonical example

Before Data Binding, you would write findViewById<TextView>(R.id.name_text).text = viewModel.userName in your Activity. With Data Binding, you declare a variable in XML: <data><variable name="viewmodel" type="com.example.MyViewModel"/></data>. Then, in your TextView, you bind it: <TextView android:text="@{viewmodel.userName}" ... />. This removes the UI manipulation code from your Activity entirely.

Interview question

Which scenario best highlights Data Binding's unique advantage over View Binding?

  • a.Optimizing layout inflation performance for complex UI hierarchies.
  • b.Reducing boilerplate code by removing findViewById calls.
  • c.Automatically updating UI elements when their associated data models change.Correct
  • d.Ensuring type-safe access to all views defined in a layout.
Why?

Data Binding excels at linking UI components directly to observable data sources in XML, enabling automatic UI updates when the data changes. While View Binding also provides type-safe access to views and eliminates findViewById calls, it does not offer this automatic data-to-UI synchronization, making it suitable only for simpler view access.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles