The Data Binding Library is a support library that allows you to bind UI
components in your layouts to data sources in your app using a declarative
format rather than programmatically.
Layouts are often defined in activities with code that calls UI framework
methods. For example, the code below calls findViewById() to find a TextView widget and bind it to the userName property of the
viewModel variable:
The following example shows how to use the Data Binding Library to assign text
to the widget directly in the layout file. This removes the need to call any of
the Java code shown above. Note the use of @{} syntax in the assignment
expression:
<TextView
android:text="@{viewmodel.userName}"/>
Binding components in the layout file lets you remove many UI framework calls in
your activities, making them simpler and easier to maintain. This can also
improve your app's performance and help prevent memory leaks and null pointer
exceptions.
Using the Data Binding Library
Use the following pages to learn how to use the Data Binding Library in your
Android apps.
The expression language allows you to write expressions that connect
variables to the views in the layout. The Data Binding Library automatically
generates the classes required to bind the views in the layout with your data
objects. The library provides features such as imports, variables, and
includes that you can use in your layouts.
These features of the library coexist seamlessly with your existing layouts.
For example, the binding variables that can be used in expressions are defined
inside a data element that is a sibling of the UI layout's root element.
Both elements are wrapped in a layout tag, as shown in the following
example:
The Data Binding Library provides classes and methods to easily observe data
for changes. You don't have to worry about refreshing the UI when the
underlying data source changes. You can make your variables or their
properties observable. The library allows you to make objects, fields, or
collections observable.
The Data Binding Library generates binding classes that are used to access the
layout's variables and views. This page shows you how to use and customize
generated binding classes.
For every layout expression, there is a binding adapter that makes the
framework calls required to set the corresponding properties or listeners. For
example, the binding adapter can take care of calling the setText() method
to set the text property or call the setOnClickListener() method to add a
listener to the click event. The most common binding adapters, such as the
adapters for the android:text property used in the examples in this page,
are available for you to use in the android.databinding.adapters package.
For a list of the common binding adapters, see
adapters.
You can also create custom adapters, as shown in the following example:
The Android Support Library includes the Architecture
Components, which you can use to
design robust, testable, and maintainable apps. You can use the Architecture
Components with the Data Binding Library to further simplify the development
of your UI.
The Data Binding Library supports two-way data binding. The notation used for
this type of binding supports the ability to receive data changes to a property
and listen to user updates to that property at the same time.
Additional resources
To learn more about data binding, consult the following
additional resources.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# Data Binding Library\nPart of [Android Jetpack](/jetpack).\n=========================================================\n\nThe Data Binding Library is a support library that allows you to bind UI\ncomponents in your layouts to data sources in your app using a declarative\nformat rather than programmatically.\n\nLayouts are often defined in activities with code that calls UI framework\nmethods. For example, the code below calls [findViewById()](/reference/android/app/Activity#findViewById(int)) to find a [TextView](/reference/android/widget/TextView) widget and bind it to the `userName` property of the\n`viewModel` variable: \n\n### Kotlin\n\n```kotlin\nfindViewById\u003cTextView\u003e(R.id.sample_text).apply {\n text = viewModel.userName\n}\n```\n\n### Java\n\n```java\nTextView textView = findViewById(R.id.sample_text);\ntextView.setText(viewModel.getUserName());\n```\n\nThe following example shows how to use the Data Binding Library to assign text\nto the widget directly in the layout file. This removes the need to call any of\nthe Java code shown above. Note the use of `@{}` syntax in the assignment\nexpression: \n\n \u003cTextView\n android:text=\"@{viewmodel.userName}\" /\u003e\n\nBinding components in the layout file lets you remove many UI framework calls in\nyour activities, making them simpler and easier to maintain. This can also\nimprove your app's performance and help prevent memory leaks and null pointer\nexceptions.\n| **Note:** In many cases, [view binding](/topic/libraries/view-binding) can provide the same benefits as data binding with simpler implementation and better performance. If you are using data binding primarily to replace `findViewById()` calls, consider using view binding instead.\n\nUsing the Data Binding Library\n------------------------------\n\nUse the following pages to learn how to use the Data Binding Library in your\nAndroid apps.\n\n[**Get started**](/topic/libraries/data-binding/start)\n: Learn how to get your development environment ready to work with the Data\n Binding Library, including support for data binding code in Android Studio.\n\n[**Layouts and binding expressions**](/topic/libraries/data-binding/expressions)\nThe expression language allows you to write expressions that connect\nvariables to the views in the layout. The Data Binding Library automatically\ngenerates the classes required to bind the views in the layout with your data\nobjects. The library provides features such as imports, variables, and\nincludes that you can use in your layouts.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThese features of the library coexist seamlessly with your existing layouts.\nFor example, the binding variables that can be used in expressions are defined\ninside a `data` element that is a sibling of the UI layout's root element.\nBoth elements are wrapped in a `layout` tag, as shown in the following\nexample:\n\n\u003cbr /\u003e\n\n \u003clayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\u003e\n \u003cdata\u003e\n \u003cvariable\n name=\"viewmodel\"\n type=\"com.myapp.data.ViewModel\" /\u003e\n \u003c/data\u003e\n \u003cConstraintLayout... /\u003e \u003c!-- UI layout's root element --\u003e\n \u003c/layout\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n[**Work with observable data objects**](/topic/libraries/data-binding/observability)\n: The Data Binding Library provides classes and methods to easily observe data\n for changes. You don't have to worry about refreshing the UI when the\n underlying data source changes. You can make your variables or their\n properties observable. The library allows you to make objects, fields, or\n collections observable.\n\n[**Generated binding classes**](/topic/libraries/data-binding/generated-binding)\n: The Data Binding Library generates binding classes that are used to access the\n layout's variables and views. This page shows you how to use and customize\n generated binding classes.\n\n[**Binding adapters**](/topic/libraries/data-binding/binding-adapters)\n: For every layout expression, there is a binding adapter that makes the\n framework calls required to set the corresponding properties or listeners. For\n example, the binding adapter can take care of calling the `setText()` method\n to set the text property or call the `setOnClickListener()` method to add a\n listener to the click event. The most common binding adapters, such as the\n adapters for the `android:text` property used in the examples in this page,\n are available for you to use in the `android.databinding.adapters` package.\n For a list of the common binding adapters, see\n [adapters](https://android.googlesource.com/platform/frameworks/data-binding/+/refs/heads/studio-master-dev/extensions/baseAdapters/src/main/java/androidx/databinding/adapters).\nYou can also create custom adapters, as shown in the following example: \n\n### Kotlin\n\n```kotlin\n@BindingAdapter(\"app:goneUnless\")\nfun goneUnless(view: View, visible: Boolean) {\n view.visibility = if (visible) View.VISIBLE else View.GONE\n}\n```\n\n### Java\n\n```java\n@BindingAdapter(\"app:goneUnless\")\npublic static void goneUnless(View view, Boolean visible) {\n view.visibility = visible ? View.VISIBLE : View.GONE;\n}\n```\n\n[**Bind layout views to Architecture Components**](/topic/libraries/data-binding/architecture)\n: The Android Support Library includes the [Architecture\n Components](/topic/libraries/architecture), which you can use to\n design robust, testable, and maintainable apps. You can use the Architecture\n Components with the Data Binding Library to further simplify the development\n of your UI.\n\n[**Two-way data binding**](/topic/libraries/data-binding/two-way)\n: The Data Binding Library supports two-way data binding. The notation used for\n this type of binding supports the ability to receive data changes to a property\n and listen to user updates to that property at the same time.\n\nAdditional resources\n--------------------\n\nTo learn more about data binding, consult the following\nadditional resources.\n\n### Samples\n\n- [Android Data Binding Library samples](https://github.com/android/databinding-samples)\n\n### Codelabs\n\n- [Android Data Binding codelab](https://codelabs.developers.google.com/codelabs/android-databinding)\n\n### Blog posts\n\n- [Android Data Binding Library --- From Observable Fields to LiveData in two steps](https://medium.com/androiddevelopers/android-data-binding-library-from-observable-fields-to-livedata-in-two-steps-690a384218f2)\n- [Data Binding --- Lessons Learnt](https://medium.com/androiddevelopers/data-binding-lessons-learnt-4fd16576b719)\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Binding adapters {:#binding-adapters}](/topic/libraries/data-binding/binding-adapters)\n- [Layouts and binding expressions](/topic/libraries/data-binding/expressions)\n- [Generated binding classes {: #binding-classes}](/topic/libraries/data-binding/generated-binding)"]]