blob: 0faa1db1bf602b4937ead0a8d8eb4ef1a8ddac21 [file] [log] [blame]
Andrew Solovay7877f692016-04-26 16:00:59 -07001page.title=Data Binding Library
2page.metaDescription=The Data Binding Library enables you to write declarative layouts.
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07003page.tags="databinding", "layouts"
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07004@jd:body
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07005
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07006<div id="qv-wrapper">
7 <div id="qv">
8 <h2>
9 In this document:
10 </h2>
11
12 <ol>
13 <li>
14 <a href="#build_environment">Build Environment</a>
15 </li>
16
17 <li>
18 <a href="#data_binding_layout_files">Data Binding Layout Files</a>
19 <ol>
20 <li>
Yigit Boyar05eaab62016-05-25 10:45:01 -070021 <a href="#writing_expressions">Writing your first set of data binding
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070022 expressions</a>
23 </li>
24
25 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070026 <a href="#data_object">Data Object</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070027 </li>
28
29 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070030 <a href="#binding_data">Binding Data</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070031 </li>
George Mount4ba18202015-07-27 12:39:28 -070032 <li>
Yigit Boyar05eaab62016-05-25 10:45:01 -070033 <a href="#event_handling">Event Handling</a>
34 <ol>
35 <li>
36 <a href="#method_references">Method References</a>
37 </li>
38 <li>
39 <a href="#listener_bindings">Listener Bindings</a>
40 </li>
41 </ol>
George Mount4ba18202015-07-27 12:39:28 -070042 </li>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070043 </ol>
44 </li>
45
46 <li>
47 <a href="#layout_details">Layout Details</a>
48 <ol>
49 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070050 <a href="#imports">Imports</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070051 </li>
52
53 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070054 <a href="#variables">Variables</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070055 </li>
56
57 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070058 <a href="#custom_binding_class_names">Custom Binding Class Names</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070059 </li>
60
61 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070062 <a href="#includes">Includes</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070063 </li>
64
65 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070066 <a href="#expression_language">Expression Language</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070067 </li>
68 </ol>
69 </li>
70
71 <li>
72 <a href="#data_objects">Data Objects</a>
73 <ol>
74 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070075 <a href="#observable_objects">Observable Objects</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070076 </li>
77
78 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070079 <a href="#observablefields">ObservableFields</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070080 </li>
81
82 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070083 <a href="#observable_collections">Observable Collections</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070084 </li>
85 </ol>
86 </li>
87
88 <li>
89 <a href="#generated_binding">Generated Binding</a>
90 <ol>
91 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070092 <a href="#creating">Creating</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070093 </li>
94
95 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -070096 <a href="#views_with_ids">Views With IDs</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -070097 </li>
98
99 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -0700100 <a href="#variables">Variables</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700101 </li>
102
103 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -0700104 <a href="#viewstubs">ViewStubs</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700105 </li>
106
107 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -0700108 <a href="#advanced_binding">Advanced Binding</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700109 </li>
110 </ol>
111 </li>
112
113 <li>
114 <a href="#attribute_setters">Attribute Setters</a>
115 <ol>
116 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -0700117 <a href="#automatic_setters">Automatic Setters</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700118 </li>
119
120 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -0700121 <a href="#renamed_setters">Renamed Setters</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700122 </li>
123
124 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -0700125 <a href="#custom_setters">Custom Setters</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700126 </li>
127 </ol>
128 </li>
129
130 <li>
131 <a href="#converters">Converters</a>
132 <ol>
133 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -0700134 <a href="#object_conversions">Object Conversions</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700135 </li>
136
137 <li>
Dirk Dougherty518651c2015-05-27 20:24:37 -0700138 <a href="#custom_conversions">Custom Conversions</a>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700139 </li>
140 </ol>
141 </li>
Daniel Yudaa41bb2015-12-08 10:58:07 -0800142
143 <li>
144 <a href="#studio_support">Android Studio Support for Data Binding</a>
145 </li>
146
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700147 </ol>
148 </div><!-- qv -->
149</div><!-- qv-wrapper -->
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700150
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700151<p>
152 This document explains how to use the Data Binding Library to write
153 declarative layouts and minimize the glue code necessary to bind your
154 application logic and layouts.
155</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700156
Yigit Boyar05eaab62016-05-25 10:45:01 -0700157<p>
158 The Data Binding Library offers both flexibility and broad compatibility —
159 it's a support library, so you can use it with all Android platform versions
160 back to <strong>Android 2.1</strong> (API level 7+).
161</p>
Dirk Dougherty08710f12015-05-28 22:01:36 -0700162
Yigit Boyar05eaab62016-05-25 10:45:01 -0700163<p>
164 To use data binding, Android Plugin for Gradle <strong>1.5.0-alpha1</strong>
smain@google.com3d5a4c92016-09-12 12:07:26 -0700165 or higher is required. See how to <a
166href="/studio/releases/gradle-plugin.html#updating-plugin">update the Android
167Plugin for Gradle</a>.
Yigit Boyar05eaab62016-05-25 10:45:01 -0700168</p>
Dirk Dougherty08710f12015-05-28 22:01:36 -0700169
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700170<h2 id="build_environment">
171 Build Environment
172</h2>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700173
sreevanis1a1d5db2015-10-30 14:44:51 -0700174<p>
Yigit Boyar05eaab62016-05-25 10:45:01 -0700175 To get started with Data Binding, download the library from the Support
176 repository in the Android SDK manager.
sreevanis0cd62032015-11-03 09:57:57 -0800177</p>
178
Yigit Boyar05eaab62016-05-25 10:45:01 -0700179<p>
180 To configure your app to use data binding, add the <code>dataBinding</code>
181 element to your <code>build.gradle</code> file in the app module.
182</p>
sreevanis1a1d5db2015-10-30 14:44:51 -0700183
Yigit Boyar05eaab62016-05-25 10:45:01 -0700184<p>
185 Use the following code snippet to configure data binding:
186</p>
sreevanis1a1d5db2015-10-30 14:44:51 -0700187<pre>
188android {
189 ....
190 dataBinding {
191 enabled = true
192 }
193}
194</pre>
Yigit Boyar05eaab62016-05-25 10:45:01 -0700195<p>
196 If you have an app module that depends on a library which uses data binding,
197 your app module must configure data binding in its <code>build.gradle</code>
198 file as well.
199</p>
Dirk Dougherty08710f12015-05-28 22:01:36 -0700200
Yigit Boyar05eaab62016-05-25 10:45:01 -0700201<p>
202 Also, make sure you are using a compatible version of Android Studio.
203 <strong>Android Studio 1.3</strong> and later provides support for data
204 binding as described in <a href="#studio_support">Android Studio Support for
205 Data Binding</a>.
Daniel Yudaa41bb2015-12-08 10:58:07 -0800206</p>
Dirk Dougherty08710f12015-05-28 22:01:36 -0700207
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700208<h2 id="data_binding_layout_files">
209 Data Binding Layout Files
210</h2>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700211
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700212<h3 id="writing_expressions">
Yigit Boyar05eaab62016-05-25 10:45:01 -0700213 Writing your first set of data binding expressions
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700214</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700215
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700216<p>
217 Data-binding layout files are slightly different and start with a root tag of
218 <strong>layout</strong> followed by a <strong>data</strong> element and a
219 <strong>view</strong> root element. This view element is what your root would
220 be in a non-binding layout file. A sample file looks like this:
221</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700222
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700223<pre>
George Mount4ba18202015-07-27 12:39:28 -0700224&lt;?xml version="1.0" encoding="utf-8"?&gt;
225&lt;layout xmlns:android="http://schemas.android.com/apk/res/android"&gt;
226 &lt;data&gt;
227 &lt;variable name="user" type="com.example.User"/&gt;
228 &lt;/data&gt;
229 &lt;LinearLayout
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700230 android:orientation="vertical"
231 android:layout_width="match_parent"
George Mount4ba18202015-07-27 12:39:28 -0700232 android:layout_height="match_parent"&gt;
233 &lt;TextView android:layout_width="wrap_content"
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700234 android:layout_height="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -0700235 android:text="&commat;{user.firstName}"/&gt;
236 &lt;TextView android:layout_width="wrap_content"
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700237 android:layout_height="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -0700238 android:text="&commat;{user.lastName}"/&gt;
239 &lt;/LinearLayout&gt;
240&lt;/layout&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700241</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700242<p>
243 The user <strong>variable</strong> within <strong>data</strong> describes a
244 property that may be used within this layout.
245</p>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700246<pre>
247&lt;<strong>variable name="user" type="com.example.User"</strong>/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700248</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700249<p>
250 Expressions within the layout are written in the attribute properties using
Mark Lu011c07b2016-07-28 11:15:11 -0700251 the "<code>&commat;{}</code>" syntax. Here, the TextView's text is set to
Yigit Boyar05eaab62016-05-25 10:45:01 -0700252 the firstName property of user:
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700253</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700254
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700255<pre>
George Mount4ba18202015-07-27 12:39:28 -0700256&lt;TextView android:layout_width="wrap_content"
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700257 android:layout_height="wrap_content"
Mark Lu011c07b2016-07-28 11:15:11 -0700258 android:text="&commat;{user.firstName}"/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700259</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700260<h3 id="data_object">
261 Data Object
262</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700263
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700264<p>
Mark Lu011c07b2016-07-28 11:15:11 -0700265 Let's assume for now that you have a plain-old Java object (POJO) for User:
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700266</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700267
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700268<pre>
George Mount4ba18202015-07-27 12:39:28 -0700269public class User {
270 public final String firstName;
271 public final String lastName;
272 public User(String firstName, String lastName) {
273 this.firstName = firstName;
274 this.lastName = lastName;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700275 }
276}
277</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700278<p>
279 This type of object has data that never changes. It is common in applications
280 to have data that is read once and never changes thereafter. It is also
281 possible to use a JavaBeans objects:
282</p>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700283<pre>
George Mount4ba18202015-07-27 12:39:28 -0700284public class User {
285 private final String firstName;
286 private final String lastName;
287 public User(String firstName, String lastName) {
288 this.firstName = firstName;
289 this.lastName = lastName;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700290 }
George Mount4ba18202015-07-27 12:39:28 -0700291 public String getFirstName() {
292 return this.firstName;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700293 }
George Mount4ba18202015-07-27 12:39:28 -0700294 public String getLastName() {
295 return this.lastName;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700296 }
297}
298</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700299<p>
300 From the perspective of data binding, these two classes are equivalent. The
Mark Lu011c07b2016-07-28 11:15:11 -0700301 expression <strong><code>&commat;{user.firstName}</code></strong> used
302 for the TextView's <strong><code>android:text</code></strong> attribute will
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700303 access the <strong><code>firstName</code></strong> field in the former class
Yigit Boyar05eaab62016-05-25 10:45:01 -0700304 and the <code>getFirstName()</code> method in the latter class.
305 Alternatively, it will also be resolved to <code>firstName()</code> if that
306 method exists.
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700307</p>
308
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700309<h3 id="binding_data">
310 Binding Data
311</h3>
312
313<p>
314 By default, a Binding class will be generated based on the name of the layout
Mark Lu011c07b2016-07-28 11:15:11 -0700315 file, converting it to Pascal case and suffixing "Binding" to it. The above
George Mount4ba18202015-07-27 12:39:28 -0700316 layout file was <code>main_activity.xml</code> so the generate class was
317 <code>MainActivityBinding</code>. This class holds all the bindings from the
Mark Lu011c07b2016-07-28 11:15:11 -0700318 layout properties (e.g. the <code>user</code> variable) to the layout's Views
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700319 and knows how to assign values for the binding expressions.The easiest means
320 for creating the bindings is to do it while inflating:
321</p>
322
323<pre>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700324&commat;Override
George Mount4ba18202015-07-27 12:39:28 -0700325protected void onCreate(Bundle savedInstanceState) {
326 super.onCreate(savedInstanceState);
327 MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
328 User user = new User("Test", "User");
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700329 binding.setUser(user);
330}
331</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700332<p>
Mark Lu011c07b2016-07-28 11:15:11 -0700333 You're done! Run the application and you'll see Test User in the UI.
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700334 Alternatively, you can get the view via:
335</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700336
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700337<pre>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700338MainActivityBinding binding = MainActivityBinding.<em>inflate</em>(getLayoutInflater());
339</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700340<p>
341 If you are using data binding items inside a ListView or RecyclerView
342 adapter, you may prefer to use:
343</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700344
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700345<pre>
George Mount4ba18202015-07-27 12:39:28 -0700346ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false);
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700347//or
348ListItemBinding binding = DataBindingUtil.<em>inflate</em>(layoutInflater, R.layout.<em><strong>list_item</strong></em>, viewGroup, <strong>false</strong>);
349</pre>
Yigit Boyar05eaab62016-05-25 10:45:01 -0700350<h3 id="event_handling">Event Handling</h3>
George Mount4ba18202015-07-27 12:39:28 -0700351<p>
Yigit Boyar05eaab62016-05-25 10:45:01 -0700352Data Binding allows you to write expressions handling events that are dispatched from the views (e.g. onClick).
353Event attribute names are governed by the name of the listener method with a few exceptions.
354For example, {@link android.view.View.OnLongClickListener} has a method {@link android.view.View.OnLongClickListener#onLongClick onLongClick()},
355so the attribute for this event is <code>android:onLongClick</code>.
356There are two ways to handle an event.
357</p>
358<ul>
359 <li>
360 <a href="#method_references">Method References</a>: In your expressions, you can reference methods that conform to the signature of the listener method. When an expression evaluates to a method reference, Data Binding wraps the method reference and owner object in a listener, and sets that listener on the target view. If the expression evaluates to null, Data Binding does not create a listener and sets a null listener instead.
361 </li>
362 <li>
363 <a href="#listener_bindings">Listener Bindings</a>: These are lambda expressions that are evaluated when the event happens.
364Data Binding always creates a listener, which it sets on the view. When the event is dispatched, the listener evaluates the lambda expression.
365 </li>
366</ul>
367<h4 id="method_references">
368 Method References
369</h4>
370<p>
371 Events can be bound to handler methods directly, similar to the way
372 <strong><code>android:onClick</code></strong> can be assigned to a method in an Activity.
373 One major advantage compared to the {@code View#onClick} attribute is that the expression
374 is processed at compile time, so if the method does not exist or its signature is not
375 correct, you receive a compile time error.</p>
376<p>
377 The major difference between Method References and Listener Bindings is that
378 the actual listener implementation is created when the data is bound, not
379 when the event is triggered. If you prefer to evaluate the expression when
380 the event happens, you should use <a href="#listener_bindings">listener
381 binding</a>.
George Mount4ba18202015-07-27 12:39:28 -0700382</p>
383<p>
384 To assign an event to its handler, use a normal binding expression, with the value
385 being the method name to call. For example, if your data object has two methods:
386</p>
387<pre>public class MyHandlers {
388 public void onClickFriend(View view) { ... }
George Mount4ba18202015-07-27 12:39:28 -0700389}
390</pre>
391<p>
392 The binding expression can assign the click listener for a View:
393</p>
394<pre>
395&lt;?xml version="1.0" encoding="utf-8"?&gt;
396&lt;layout xmlns:android="http://schemas.android.com/apk/res/android"&gt;
397 &lt;data&gt;
398 &lt;variable name="handlers" type="com.example.Handlers"/&gt;
399 &lt;variable name="user" type="com.example.User"/&gt;
400 &lt;/data&gt;
401 &lt;LinearLayout
402 android:orientation="vertical"
403 android:layout_width="match_parent"
404 android:layout_height="match_parent"&gt;
405 &lt;TextView android:layout_width="wrap_content"
406 android:layout_height="wrap_content"
407 android:text="&commat;{user.firstName}"
Yigit Boyar05eaab62016-05-25 10:45:01 -0700408 android:onClick="&commat;{handlers::onClickFriend}"/&gt;
George Mount4ba18202015-07-27 12:39:28 -0700409 &lt;/LinearLayout&gt;
410&lt;/layout&gt;
411</pre>
Yigit Boyar05eaab62016-05-25 10:45:01 -0700412<p>
413Note that the signature of the method in the expression must exactly match the signature of the method in the
414Listener object.
415</p>
416<h4 id="listener_bindings">
417 Listener Bindings
418</h4>
419<p>
420 Listener Bindings are binding expressions that run when an event happens.
421 They are similar to method references, but they let you run arbitrary data
422 binding expressions. This feature is available with Android Gradle Plugin for Gradle
423 version 2.0 and later.
424</p>
425<p>
426 In method references, the parameters of the method must
427 match the parameters of the event listener. In Listener Bindings, only your
428 return value must match the expected return value of the listener (unless it
429 is expecting void).
430 For example, you can have a presenter class that has the following method:
431</p>
432<pre>
433public class Presenter {
434 public void onSaveClick(Task task){}
435}
436</pre>
437 Then you can bind the click event to your class as follows:
438<pre>
Mark Lu011c07b2016-07-28 11:15:11 -0700439 &lt;?xml version="1.0" encoding="utf-8"?&gt;
440 &lt;layout xmlns:android="http://schemas.android.com/apk/res/android"&gt;
Yigit Boyar05eaab62016-05-25 10:45:01 -0700441 &lt;data&gt;
Mark Lu011c07b2016-07-28 11:15:11 -0700442 &lt;variable name="task" type="com.android.example.Task" /&gt;
443 &lt;variable name="presenter" type="com.android.example.Presenter" /&gt;
Yigit Boyar05eaab62016-05-25 10:45:01 -0700444 &lt;/data&gt;
Mark Lu011c07b2016-07-28 11:15:11 -0700445 &lt;LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"&gt;
446 &lt;Button android:layout_width="wrap_content" android:layout_height="wrap_content"
447 android:onClick="@{() -&gt; presenter.onSaveClick(task)}" /&gt;
Yigit Boyar05eaab62016-05-25 10:45:01 -0700448 &lt;/LinearLayout&gt;
449 &lt;/layout&gt;
450</pre>
451<p>
452 Listeners are represented by lambda expressions that are allowed only as root
453 elements of your expressions. When a callback is used in an expression, Data
454 Binding automatically creates the necessary listener and registers for the
455 event. When the view fires the event, Data Binding evaluates the given
456 expression. As in regular binding expressions, you still get the null and
457 thread safety of Data Binding while these listener expressions are being
458 evaluated.
459</p>
460<p>
461 Note that in the example above, we haven't defined the {@code view} parameter
462 that is passed into {@link
463 android.view.View.OnClickListener#onClick(android.view.View view)}. Listener
464 bindings provide two choices for listener parameters: you can either ignore
465 all parameters to the method or name all of them. If you prefer to name the
466 parameters, you can use them in your expression. For example, the expression
467 above could be written as:
468</p>
469<pre>
Mark Lu011c07b2016-07-28 11:15:11 -0700470 android:onClick="@{(view) -&gt; presenter.onSaveClick(task)}"
Yigit Boyar05eaab62016-05-25 10:45:01 -0700471</pre>
472Or if you wanted to use the parameter in the expression, it could work as follows:
473<pre>
474public class Presenter {
475 public void onSaveClick(View view, Task task){}
476}
477</pre>
478<pre>
Mark Lu011c07b2016-07-28 11:15:11 -0700479 android:onClick="@{(theView) -&gt; presenter.onSaveClick(theView, task)}"
Yigit Boyar05eaab62016-05-25 10:45:01 -0700480</pre>
481You can use a lambda expression with more than one parameter:
482<pre>
483public class Presenter {
484 public void onCompletedChanged(Task task, boolean completed){}
485}
486</pre>
487<pre>
Mark Lu011c07b2016-07-28 11:15:11 -0700488 &lt;CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content"
489 android:onCheckedChanged="@{(cb, isChecked) -&gt; presenter.completeChanged(task, isChecked)}" /&gt;
Yigit Boyar05eaab62016-05-25 10:45:01 -0700490</pre>
491<p>
492 If the event you are listening to returns a value whose type is not {@code
493 void}, your expressions must return the same type of value as well. For
494 example, if you want to listen for the long click event, your expression
495 should return {@code boolean}.
496</p>
497<pre>
498public class Presenter {
499 public boolean onLongClick(View view, Task task){}
500}
501</pre>
502<pre>
Mark Lu011c07b2016-07-28 11:15:11 -0700503 android:onLongClick="@{(theView) -&gt; presenter.onLongClick(theView, task)}"
Yigit Boyar05eaab62016-05-25 10:45:01 -0700504</pre>
505<p>
506If the expression cannot be evaluated due to {@code null} objects, Data Binding returns
507the default Java value for that type. For example, {@code null} for reference types, {@code 0} for {@code int},
508{@code false} for {@code boolean}, etc.
509</p>
510<p>
511If you need to use an expression with a predicate (e.g. ternary), you can use
512{@code void} as a symbol.
513</p>
514<pre>
Mark Lu011c07b2016-07-28 11:15:11 -0700515 android:onClick="@{(v) -&gt; v.isVisible() ? doSomething() : void}"
Yigit Boyar05eaab62016-05-25 10:45:01 -0700516</pre>
517
518<h5>Avoid Complex Listeners</h5>
519Listener expressions are very powerful and can make your code very easy to read.
Mark Luc4a01392016-07-18 10:42:11 -0700520On the other hand, listeners containing complex expressions make your layouts hard to read and unmaintainable.
521These expressions should be as simple as passing available data from your UI to your callback method. You should implement
Yigit Boyar05eaab62016-05-25 10:45:01 -0700522any business logic inside the callback method that you invoked from the listener expression.
George Mount9b332f32016-01-28 14:31:25 -0800523
524<p>
525 Some specialized click event handlers exist and they need an attribute other than
526 <code>android:onClick</code> to avoid a conflict. The following attributes have been created
527 to avoid such conflicts:
528</p>
529
530<table>
531 <tr>
532 <th>Class</th>
533 <th>Listener Setter</th>
534 <th>Attribute</th>
535 </tr>
536 <tr>
537 <td>{@link android.widget.SearchView}</td>
538 <td>{@link android.widget.SearchView#setOnSearchClickListener}</td>
539 <td><code>android:onSearchClick</code></td>
540 </tr>
541 <tr>
542 <td>{@link android.widget.ZoomControls}</td>
543 <td>{@link android.widget.ZoomControls#setOnZoomInClickListener}</td>
544 <td><code>android:onZoomIn</code></td>
545 </tr>
546 <tr>
547 <td>{@link android.widget.ZoomControls}</td>
548 <td>{@link android.widget.ZoomControls#setOnZoomOutClickListener}</td>
549 <td><code>android:onZoomOut</code></td>
550 </tr>
551</table>
552
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700553<h2 id="layout_details">
554 Layout Details
555</h2>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700556
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700557<h3 id="imports">
558 Imports
559</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700560
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700561<p>
562 Zero or more <strong><code>import</code></strong> elements may be used inside
563 the <strong><code>data</code></strong> element. These allow easy reference to
564 classes inside your layout file, just like in Java.
565</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700566
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700567<pre>
George Mount4ba18202015-07-27 12:39:28 -0700568&lt;data&gt;
569 &lt;import type="android.view.View"/&gt;
570&lt;/data&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700571</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700572<p>
573 Now, View may be used within your binding expression:
574</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700575
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700576<pre>
George Mount4ba18202015-07-27 12:39:28 -0700577&lt;TextView
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700578 android:text="&commat;{user.lastName}"
579 android:layout_width="wrap_content"
580 android:layout_height="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -0700581 android:visibility="&commat;{user.isAdult ? View.VISIBLE : View.GONE}"/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700582</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700583<p>
584 When there are class name conflicts, one of the classes may be renamed to an
Mark Lu011c07b2016-07-28 11:15:11 -0700585 "alias:"
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700586</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700587
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700588<pre>
George Mount4ba18202015-07-27 12:39:28 -0700589&lt;import type="android.view.View"/&gt;
590&lt;import type="com.example.real.estate.View"
591 alias="Vista"/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700592</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700593<p>
594 Now, <strong><code>Vista</code></strong> may be used to reference the
595 <code>com.example.real.estate.View</code> and
596 <strong><code>View</code></strong> may be used to reference
597 <code>android.view.View</code> within the layout file. Imported types may be
598 used as type references in variables and expressions:
599</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700600
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700601<pre>
George Mount4ba18202015-07-27 12:39:28 -0700602&lt;data&gt;
603 &lt;import type="com.example.User"/&gt;
604 &lt;import type="java.util.List"/&gt;
605 &lt;variable name="user" type="User"/&gt;
Mark Lub1b5efd2016-08-04 14:45:42 -0700606 &lt;variable name="userList" type="List&amp;lt;User&amp;gt;"/&gt;
George Mount4ba18202015-07-27 12:39:28 -0700607&lt;/data&gt;
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700608</pre>
Dirk Dougherty08710f12015-05-28 22:01:36 -0700609<p class="caution">
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700610 <strong>Note</strong>: Android Studio does not yet handle imports so the
611 autocomplete for imported variables may not work in your IDE. Your
612 application will still compile fine and you can work around the IDE issue by
613 using fully qualified names in your variable definitions.
614</p>
615
616<pre>
George Mount4ba18202015-07-27 12:39:28 -0700617&lt;TextView
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700618 android:text="&commat;{((User)(user.connection)).lastName}"
619 android:layout_width="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -0700620 android:layout_height="wrap_content"/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700621</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700622<p>
623 Imported types may also be used when referencing static fields and methods in
624 expressions:
625</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700626
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700627<pre>
George Mount4ba18202015-07-27 12:39:28 -0700628&lt;data&gt;
629 &lt;import type="com.example.MyStringUtils"/&gt;
630 &lt;variable name="user" type="com.example.User"/&gt;
631&lt;/data&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700632…
George Mount4ba18202015-07-27 12:39:28 -0700633&lt;TextView
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700634 android:text="&commat;{MyStringUtils.capitalize(user.lastName)}"
635 android:layout_width="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -0700636 android:layout_height="wrap_content"/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700637</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700638<p>
639 Just as in Java, <code>java.lang.*</code> is imported automatically.
640</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700641
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700642<h3 id="variables">
643 Variables
644</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700645
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700646<p>
647 Any number of <strong><code>variable</code></strong> elements may be used
648 inside the <strong><code>data</code></strong> element. Each
649 <strong><code>variable</code></strong> element describes a property that may
650 be set on the layout to be used in binding expressions within the layout
651 file.
652</p>
653
654<pre>
George Mount4ba18202015-07-27 12:39:28 -0700655&lt;data&gt;
656 &lt;import type="android.graphics.drawable.Drawable"/&gt;
657 &lt;variable name="user" type="com.example.User"/&gt;
658 &lt;variable name="image" type="Drawable"/&gt;
659 &lt;variable name="note" type="String"/&gt;
660&lt;/data&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700661</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700662<p>
663 The variable types are inspected at compile time, so if a variable implements
George Mount4ba18202015-07-27 12:39:28 -0700664 {@link android.databinding.Observable} or is an <a href=
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700665 "#observable_collections">observable collection</a>, that should be reflected
666 in the type. If the variable is a base class or interface that does not
667 implement the Observable* interface, the variables will <strong>not
668 be</strong> observed!
669</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700670
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700671<p>
672 When there are different layout files for various configurations (e.g.
673 landscape or portrait), the variables will be combined. There must not be
674 conflicting variable definitions between these layout files.
675</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700676
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700677<p>
678 The generated binding class will have a setter and getter for each of the
679 described variables. The variables will take the default Java values until
680 the setter is called &mdash; <code>null</code> for reference types,
681 <code>0</code> for <code>int</code>, <code>false</code> for
682 <code>boolean</code>, etc.
683</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700684
George Mount9b332f32016-01-28 14:31:25 -0800685<p>
686 A special variable named <code>context</code> is generated for use in binding
687 expressions as needed. The value for <code>context</code> is the
688 <code>Context</code> from the root View's {@link android.view.View#getContext}.
689 The <code>context</code> variable will be overridden by an explicit variable
690 declaration with that name.
691</p>
692
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700693<h3 id="custom_binding_class_names">
694 Custom Binding Class Names
695</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700696
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700697<p>
698 By default, a Binding class is generated based on the name of the layout
699 file, starting it with upper-case, removing underscores ( _ ) and
Mark Lu011c07b2016-07-28 11:15:11 -0700700 capitalizing the following letter and then suffixing "Binding". This class
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700701 will be placed in a databinding package under the module package. For
702 example, the layout file <code>contact_item.xml</code> will generate
703 <code>ContactItemBinding</code>. If the module package is
704 <code>com.example.my.app</code>, then it will be placed in
705 <code>com.example.my.app.databinding</code>.
706</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700707
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700708<p>
709 Binding classes may be renamed or placed in different packages by adjusting
710 the <strong><code>class</code></strong> attribute of the
711 <strong><code>data</code></strong> element. For example:
712</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700713
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700714<pre>
George Mount4ba18202015-07-27 12:39:28 -0700715&lt;data class="ContactItem"&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700716 ...
George Mount4ba18202015-07-27 12:39:28 -0700717&lt;/data&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700718</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700719<p>
720 This generates the binding class as <code>ContactItem</code> in the
721 databinding package in the module package. If the class should be generated
722 in a different package within the module package, it may be prefixed with
Mark Lu011c07b2016-07-28 11:15:11 -0700723 ".":
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700724</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700725
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700726<pre>
George Mount4ba18202015-07-27 12:39:28 -0700727&lt;data class=".ContactItem"&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700728 ...
George Mount4ba18202015-07-27 12:39:28 -0700729&lt;/data&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700730</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700731<p>
732 In this case, <code>ContactItem</code> is generated in the module package
733 directly. Any package may be used if the full package is provided:
734</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700735
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700736<pre>
George Mount4ba18202015-07-27 12:39:28 -0700737&lt;data class="com.example.ContactItem"&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700738 ...
George Mount4ba18202015-07-27 12:39:28 -0700739&lt;/data&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700740</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700741<h3 id="includes">
742 Includes
743</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700744
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700745<p>
Mark Lu011c07b2016-07-28 11:15:11 -0700746 Variables may be passed into an included layout's binding from the
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700747 containing layout by using the application namespace and the variable name in
748 an attribute:
749</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700750
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700751<pre>
George Mount4ba18202015-07-27 12:39:28 -0700752&lt;?xml version="1.0" encoding="utf-8"?&gt;
753&lt;layout xmlns:android="http://schemas.android.com/apk/res/android"
754 xmlns:bind="http://schemas.android.com/apk/res-auto"&gt;
755 &lt;data&gt;
756 &lt;variable name="user" type="com.example.User"/&gt;
757 &lt;/data&gt;
758 &lt;LinearLayout
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700759 android:orientation="vertical"
760 android:layout_width="match_parent"
George Mount4ba18202015-07-27 12:39:28 -0700761 android:layout_height="match_parent"&gt;
762 &lt;include layout="&commat;layout/name"
763 bind:user="&commat;{user}"/&gt;
764 &lt;include layout="&commat;layout/contact"
765 bind:user="&commat;{user}"/&gt;
766 &lt;/LinearLayout&gt;
767&lt;/layout&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700768</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700769<p>
770 Here, there must be a <code>user</code> variable in both the
771 <code>name.xml</code> and <code>contact.xml</code> layout files.
772</p>
George Mount4ba18202015-07-27 12:39:28 -0700773<p>
774 Data binding does not support include as a direct child of a merge element. For example,
775 <strong>the following layout is not supported:</strong>
776</p>
777<pre>
778&lt;?xml version="1.0" encoding="utf-8"?&gt;
779&lt;layout xmlns:android="http://schemas.android.com/apk/res/android"
780 xmlns:bind="http://schemas.android.com/apk/res-auto"&gt;
781 &lt;data&gt;
782 &lt;variable name="user" type="com.example.User"/&gt;
783 &lt;/data&gt;
784 &lt;merge&gt;
785 &lt;include layout="&commat;layout/name"
786 bind:user="&commat;{user}"/&gt;
787 &lt;include layout="&commat;layout/contact"
788 bind:user="&commat;{user}"/&gt;
789 &lt;/merge&gt;
790&lt;/layout&gt;
791</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700792<h3 id="expression_language">
793 Expression Language
794</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700795
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700796<h4 id="common_features">
797 Common Features
798</h4>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700799
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700800<p>
801 The expression language looks a lot like a Java expression. These are the
802 same:
803</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700804
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700805<ul>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700806 <li>Mathematical <strong><code>+ - / * %</code></strong>
807 </li>
808
809 <li>String concatenation <strong><code>+</code></strong>
810 </li>
811
812 <li>
George Mount4ba18202015-07-27 12:39:28 -0700813 Logical <strong><code>&& ||</code></strong>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700814 </li>
815
George Mount4ba18202015-07-27 12:39:28 -0700816 <li>Binary <strong><code>& | ^</code></strong>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700817 </li>
818
819 <li>Unary <strong><code>+ - ! ~</code></strong>
820 </li>
821
822 <li>Shift <strong><code>&gt;&gt; &gt;&gt;&gt; &lt;&lt;</code></strong>
823 </li>
824
825 <li>Comparison <strong><code>== &gt; &lt; &gt;= &lt;=</code></strong>
826 </li>
827
828 <li>
829 <strong><code>instanceof</code></strong>
830 </li>
831
832 <li>Grouping <strong><code>()</code></strong>
833 </li>
834
835 <li>Literals - character, String, numeric, <strong><code>null</code></strong>
836 </li>
837
838 <li>Cast
839 </li>
840
841 <li>Method calls
842 </li>
843
844 <li>Field access
845 </li>
846
847 <li>Array access <strong><code>[]</code></strong>
848 </li>
849
850 <li>Ternary operator <strong><code>?:</code></strong>
851 </li>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700852</ul>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700853
854<p>
855 Examples:
856</p>
857
858<pre>
George Mount4ba18202015-07-27 12:39:28 -0700859android:text="&commat;{String.valueOf(index + 1)}"
Mark Lu011c07b2016-07-28 11:15:11 -0700860android:visibility="&commat;{age &lt; 13 ? View.GONE : View.VISIBLE}"
861android:transitionName='&commat;{"image_" + id}'
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700862</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700863<h4 id="missing_operations">
864 Missing Operations
865</h4>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700866
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700867<p>
868 A few operations are missing from the expression syntax that you can use in
869 Java.
870</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700871
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700872<ul>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700873 <li>
874 <strong><code>this</code></strong>
875 </li>
876
877 <li>
878 <strong><code>super</code></strong>
879 </li>
880
881 <li>
882 <strong><code>new</code></strong>
883 </li>
884
885 <li>Explicit generic invocation
886 </li>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700887</ul>
888
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700889<h4 id="null_coalescing_operator">
890 Null Coalescing Operator
891</h4>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700892
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700893<p>
894 The null coalescing operator (<strong><code>??</code></strong>) chooses the
895 left operand if it is not null or the right if it is null.
896</p>
897
898<pre>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700899<strong>android:text="&commat;{user.displayName ?? user.lastName}"</strong>
900</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700901<p>
902 This is functionally equivalent to:
903</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700904
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700905<pre>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700906<strong>android:text="&commat;{user.displayName != null ? user.displayName : user.lastName}"</strong>
907</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700908<h4 id="property_reference">
909 Property Reference
910</h4>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700911
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700912<p>
913 The first was already discussed in the <a href=
914 "#writing_your_first_data_binding_expressions">Writing your first data
915 binding expressions</a> above: short form JavaBean references. When an
916 expression references a property on a class, it uses the same format for
917 fields, getters, and ObservableFields.
918</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700919
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700920<pre>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700921<strong>android:text="&commat;{user.lastName}"</strong>
922</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700923<h4>
924 Avoiding NullPointerException
925</h4>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700926
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700927<p>
928 Generated data binding code automatically checks for nulls and avoid null
929 pointer exceptions. For example, in the expression
930 <code>&commat;{user.name}</code>, if <code>user</code> is null,
931 <code>user.name</code> will be assigned its default value (null). If you were
932 referencing <code>user.age</code>, where age is an <code>int</code>, then it
933 would default to 0.
934</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700935
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700936<h4 id="collections">
937 Collections
938</h4>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700939
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700940<p>
941 Common collections: arrays, lists, sparse lists, and maps, may be accessed
942 using the <code>[]</code> operator for convenience.
943</p>
944
945<pre>
George Mount4ba18202015-07-27 12:39:28 -0700946&lt;data&gt;
947 &lt;import type="android.util.SparseArray"/&gt;
948 &lt;import type="java.util.Map"/&gt;
949 &lt;import type="java.util.List"/&gt;
Mark Lub1b5efd2016-08-04 14:45:42 -0700950 &lt;variable name="list" type="List&amp;lt;String&amp;gt;"/&gt;
951 &lt;variable name="sparse" type="SparseArray&amp;lt;String&amp;gt;"/&gt;
952 &lt;variable name="map" type="Map&amp;lt;String, String&amp;gt;"/&gt;
George Mount4ba18202015-07-27 12:39:28 -0700953 &lt;variable name="index" type="int"/&gt;
954 &lt;variable name="key" type="String"/&gt;
955&lt;/data&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700956…
George Mount4ba18202015-07-27 12:39:28 -0700957android:text="&commat;{list[index]}"
958…
959android:text="&commat;{sparse[index]}"
960…
961android:text="&commat;{map[key]}"
962
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700963</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700964<h4 id="string_literals">
965 String Literals
966</h4>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700967
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700968<p>
969 When using single quotes around the attribute value, it is easy to use double
970 quotes in the expression:
971</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700972
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700973<pre>
Mark Lu011c07b2016-07-28 11:15:11 -0700974android:text='&commat;{map["firstName"]}'
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700975</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700976<p>
977 It is also possible to use double quotes to surround the attribute value.
Mark Lu011c07b2016-07-28 11:15:11 -0700978 When doing so, String literals should either use the ' or back quote
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700979 (`).
980</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700981
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700982<pre>
George Mount4ba18202015-07-27 12:39:28 -0700983android:text="&commat;{map[`firstName`}"
Mark Lu011c07b2016-07-28 11:15:11 -0700984android:text="&commat;{map['firstName']}"
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700985</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700986<h4 id="resources">
987 Resources
988</h4>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700989
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700990<p>
991 It is possible to access resources as part of expressions using the normal
992 syntax:
993</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700994
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700995<pre>
George Mount4ba18202015-07-27 12:39:28 -0700996android:padding="&commat;{large? &commat;dimen/largePadding : &commat;dimen/smallPadding}"
Dirk Dougherty493ff3c2015-05-26 16:52:23 -0700997</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -0700998<p>
999 Format strings and plurals may be evaluated by providing parameters:
1000</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001001
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001002<pre>
George Mount4ba18202015-07-27 12:39:28 -07001003android:text="&commat;{&commat;string/nameFormat(firstName, lastName)}"
1004android:text="&commat;{&commat;plurals/banana(bananaCount)}"
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001005</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001006<p>
1007 When a plural takes multiple parameters, all parameters should be passed:
1008</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001009
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001010<pre>
1011
1012 Have an orange
1013 Have %d oranges
1014
George Mount4ba18202015-07-27 12:39:28 -07001015android:text="&commat;{&commat;plurals/orange(orangeCount, orangeCount)}"
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001016</pre>
1017<p>
1018 Some resources require explicit type evaluation.
1019</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001020
1021<table>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001022 <tr>
1023 <th>
1024 Type
1025 </th>
1026 <th>
1027 Normal Reference
1028 </th>
1029 <th>
1030 Expression Reference
1031 </th>
1032 </tr>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001033
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001034 <tr>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001035 <td>
George Mount4ba18202015-07-27 12:39:28 -07001036 String[]
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001037 </td>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001038 <td>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001039 &commat;array
1040 </td>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001041 <td>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001042 &commat;stringArray
1043 </td>
1044 </tr>
1045
1046 <tr>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001047 <td>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001048 int[]
1049 </td>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001050 <td>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001051 &commat;array
1052 </td>
1053 <td>
1054 &commat;intArray
1055 </td>
1056 </tr>
1057
1058 <tr>
1059 <td>
1060 TypedArray
1061 </td>
1062 <td>
1063 &commat;array
1064 </td>
1065 <td>
1066 &commat;typedArray
1067 </td>
1068 </tr>
1069
1070 <tr>
1071 <td>
1072 Animator
1073 </td>
1074 <td>
1075 &commat;animator
1076 </td>
1077 <td>
1078 &commat;animator
1079 </td>
1080 </tr>
1081
1082 <tr>
1083 <td>
1084 StateListAnimator
1085 </td>
1086 <td>
1087 &commat;animator
1088 </td>
1089 <td>
1090 &commat;stateListAnimator
1091 </td>
1092 </tr>
1093
1094 <tr>
1095 <td>
1096 color <code>int</code>
1097 </td>
1098 <td>
George Mount4ba18202015-07-27 12:39:28 -07001099 &commat;color
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001100 </td>
1101 <td>
1102 &commat;color
1103 </td>
1104 </tr>
1105
1106 <tr>
1107 <td>
1108 ColorStateList
1109 </td>
1110 <td>
1111 &commat;color
1112 </td>
1113 <td>
1114 &commat;colorStateList
1115 </td>
1116 </tr>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001117</table>
1118
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001119<h2 id="data_objects">
1120 Data Objects
1121</h2>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001122
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001123<p>
1124 Any plain old Java object (POJO) may be used for data binding, but modifying
1125 a POJO will not cause the UI to update. The real power of data binding can be
1126 used by giving your data objects the ability to notify when data changes.
1127 There are three different data change notification mechanisms,
George Mount4ba18202015-07-27 12:39:28 -07001128 <a href="#observable_objects">Observable objects</a>,
1129 <a href="#observablefields">observable fields</a>, and
1130 <a href="#observable_collections">observable collection</a>s.
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001131</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001132
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001133<p>
1134 When one of these observable data object is bound to the UI and a property of
1135 the data object changes, the UI will be updated automatically.
1136</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001137
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001138<h3 id="observable_objects">
1139 Observable Objects
1140</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001141
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001142<p>
George Mount4ba18202015-07-27 12:39:28 -07001143 A class implementing the {@link android.databinding.Observable} interface
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001144 will allow the binding to attach a single listener to a bound object to
1145 listen for changes of all properties on that object.
1146</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001147
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001148<p>
George Mount4ba18202015-07-27 12:39:28 -07001149 The {@link android.databinding.Observable} interface has a mechanism to add and remove
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001150 listeners, but notifying is up to the developer. To make development easier,
George Mount4ba18202015-07-27 12:39:28 -07001151 a base class, {@link android.databinding.BaseObservable}, was created to implement the
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001152 listener registration mechanism. The data class implementer is still
1153 responsible for notifying when the properties change. This is done by
George Mount4ba18202015-07-27 12:39:28 -07001154 assigning a {@link android.databinding.Bindable} annotation to the getter and notifying in
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001155 the setter.
1156</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001157
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001158<pre>
George Mount4ba18202015-07-27 12:39:28 -07001159private static class User extends BaseObservable {
1160 private String firstName;
1161 private String lastName;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001162 &commat;Bindable
George Mount4ba18202015-07-27 12:39:28 -07001163 public String getFirstName() {
1164 return this.firstName;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001165 }
1166 &commat;Bindable
George Mount4ba18202015-07-27 12:39:28 -07001167 public String getLastName() {
1168 return this.lastName;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001169 }
George Mount4ba18202015-07-27 12:39:28 -07001170 public void setFirstName(String firstName) {
1171 this.firstName = firstName;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001172 notifyPropertyChanged(BR.firstName);
1173 }
George Mount4ba18202015-07-27 12:39:28 -07001174 public void setLastName(String lastName) {
1175 this.lastName = lastName;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001176 notifyPropertyChanged(BR.lastName);
1177 }
1178}
1179</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001180<p>
George Mount4ba18202015-07-27 12:39:28 -07001181 The {@link android.databinding.Bindable} annotation generates an entry in the BR class file
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001182 during compilation. The BR class file will be generated in the module
George Mount4ba18202015-07-27 12:39:28 -07001183 package. If the base class for data classes cannot be changed, the
1184 {@link android.databinding.Observable} interface may be implemented using the convenient
1185 {@link android.databinding.PropertyChangeRegistry} to store and notify listeners
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001186 efficiently.
1187</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001188
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001189<h3 id="observablefields">
1190 ObservableFields
1191</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001192
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001193<p>
George Mount4ba18202015-07-27 12:39:28 -07001194 A little work is involved in creating {@link android.databinding.Observable} classes, so
1195 developers who want to save time or have few properties may use
1196 {@link android.databinding.ObservableField} and its siblings
1197 {@link android.databinding.ObservableBoolean},
1198 {@link android.databinding.ObservableByte},
1199 {@link android.databinding.ObservableChar},
1200 {@link android.databinding.ObservableShort},
1201 {@link android.databinding.ObservableInt},
1202 {@link android.databinding.ObservableLong},
1203 {@link android.databinding.ObservableFloat},
1204 {@link android.databinding.ObservableDouble}, and
1205 {@link android.databinding.ObservableParcelable}.
1206 <code>ObservableFields</code> are self-contained observable objects that have a single
1207 field. The primitive versions avoid boxing and unboxing during access operations.
1208 To use, create a public final field in the data class:
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001209</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001210
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001211<pre>
George Mount4ba18202015-07-27 12:39:28 -07001212private static class User {
1213 public final ObservableField&lt;String&gt; firstName =
1214 new ObservableField&lt;&gt;();
1215 public final ObservableField&lt;String&gt; lastName =
1216 new ObservableField&lt;&gt;();
1217 public final ObservableInt age = new ObservableInt();
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001218}
1219</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001220<p>
Mark Lu011c07b2016-07-28 11:15:11 -07001221 That's it! To access the value, use the set and get accessor methods:
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001222</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001223
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001224<pre>
George Mount4ba18202015-07-27 12:39:28 -07001225user.firstName.set("Google");
1226int age = user.age.get();
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001227</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001228<h3 id="observable_collections">
1229 Observable Collections
1230</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001231
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001232<p>
1233 Some applications use more dynamic structures to hold data. Observable
George Mount4ba18202015-07-27 12:39:28 -07001234 collections allow keyed access to these data objects.
1235 {@link android.databinding.ObservableArrayMap} is
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001236 useful when the key is a reference type, such as String.
1237</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001238
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001239<pre>
George Mount4ba18202015-07-27 12:39:28 -07001240ObservableArrayMap&lt;String, Object&gt; user = new ObservableArrayMap&lt;&gt;();
1241user.put("firstName", "Google");
1242user.put("lastName", "Inc.");
1243user.put("age", 17);
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001244</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001245<p>
1246 In the layout, the map may be accessed through the String keys:
1247</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001248
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001249<pre>
George Mount4ba18202015-07-27 12:39:28 -07001250&lt;data&gt;
1251 &lt;import type="android.databinding.ObservableMap"/&gt;
Mark Lub1b5efd2016-08-04 14:45:42 -07001252 &lt;variable name="user" type="ObservableMap&amp;lt;String, Object&amp;gt;"/&gt;
George Mount4ba18202015-07-27 12:39:28 -07001253&lt;/data&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001254…
George Mount4ba18202015-07-27 12:39:28 -07001255&lt;TextView
Mark Lu011c07b2016-07-28 11:15:11 -07001256 android:text='&commat;{user["lastName"]}'
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001257 android:layout_width="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -07001258 android:layout_height="wrap_content"/&gt;
1259&lt;TextView
Mark Lu011c07b2016-07-28 11:15:11 -07001260 android:text='&commat;{String.valueOf(1 + (Integer)user["age"])}'
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001261 android:layout_width="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -07001262 android:layout_height="wrap_content"/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001263</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001264<p>
George Mount4ba18202015-07-27 12:39:28 -07001265 {@link android.databinding.ObservableArrayList} is useful when the key is an integer:
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001266</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001267
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001268<pre>
George Mount4ba18202015-07-27 12:39:28 -07001269ObservableArrayList&lt;Object&gt; user = new ObservableArrayList&lt;&gt;();
1270user.add("Google");
1271user.add("Inc.");
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001272user.add(17);
1273</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001274<p>
1275 In the layout, the list may be accessed through the indices:
1276</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001277
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001278<pre>
George Mount4ba18202015-07-27 12:39:28 -07001279&lt;data&gt;
1280 &lt;import type="android.databinding.ObservableList"/&gt;
1281 &lt;import type="com.example.my.app.Fields"/&gt;
Mark Lub1b5efd2016-08-04 14:45:42 -07001282 &lt;variable name="user" type="ObservableList&amp;lt;Object&amp;gt;"/&gt;
George Mount4ba18202015-07-27 12:39:28 -07001283&lt;/data&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001284…
George Mount4ba18202015-07-27 12:39:28 -07001285&lt;TextView
Mark Lu011c07b2016-07-28 11:15:11 -07001286 android:text='&commat;{user[Fields.LAST_NAME]}'
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001287 android:layout_width="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -07001288 android:layout_height="wrap_content"/&gt;
1289&lt;TextView
Mark Lu011c07b2016-07-28 11:15:11 -07001290 android:text='&commat;{String.valueOf(1 + (Integer)user[Fields.AGE])}'
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001291 android:layout_width="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -07001292 android:layout_height="wrap_content"/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001293</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001294<h2 id="generated_binding">
1295 Generated Binding
1296</h2>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001297
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001298<p>
1299 The generated binding class links the layout variables with the Views within
1300 the layout. As discussed earlier, the name and package of the Binding may be
1301 <a href="#custom_binding_class_names">customized</a>. The Generated binding
George Mount4ba18202015-07-27 12:39:28 -07001302 classes all extend {@link android.databinding.ViewDataBinding}.
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001303</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001304
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001305<h3 id="creating">
1306 Creating
1307</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001308
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001309<p>
1310 The binding should be created soon after inflation to ensure that the View
1311 hierarchy is not disturbed prior to binding to the Views with expressions
1312 within the layout. There are a few ways to bind to a layout. The most common
1313 is to use the static methods on the Binding class.The inflate method inflates
1314 the View hierarchy and binds to it all it one step. There is a simpler
George Mount4ba18202015-07-27 12:39:28 -07001315 version that only takes a {@link android.view.LayoutInflater} and one that takes a
1316 {@link android.view.ViewGroup} as well:
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001317</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001318
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001319<pre>
George Mount4ba18202015-07-27 12:39:28 -07001320MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater);
1321MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater, viewGroup, false);
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001322</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001323<p>
1324 If the layout was inflated using a different mechanism, it may be bound
1325 separately:
1326</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001327
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001328<pre>
George Mount4ba18202015-07-27 12:39:28 -07001329MyLayoutBinding binding = MyLayoutBinding.bind(viewRoot);
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001330</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001331<p>
1332 Sometimes the binding cannot be known in advance. In such cases, the binding
George Mount4ba18202015-07-27 12:39:28 -07001333 can be created using the {@link android.databinding.DataBindingUtil} class:
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001334</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001335
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001336<pre>
George Mount4ba18202015-07-27 12:39:28 -07001337ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater, layoutId,
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001338 parent, attachToParent);
George Mount4ba18202015-07-27 12:39:28 -07001339ViewDataBinding binding = DataBindingUtil.bindTo(viewRoot, layoutId);
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001340</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001341<h3 id="views_with_ids">
1342 Views With IDs
1343</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001344
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001345<p>
1346 A public final field will be generated for each View with an ID in the
1347 layout. The binding does a single pass on the View hierarchy, extracting the
1348 Views with IDs. This mechanism can be faster than calling findViewById for
1349 several Views. For example:
1350</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001351
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001352<pre>
George Mount4ba18202015-07-27 12:39:28 -07001353&lt;layout xmlns:android="http://schemas.android.com/apk/res/android"&gt;
1354 &lt;data&gt;
1355 &lt;variable name="user" type="com.example.User"/&gt;
1356 &lt;/data&gt;
1357 &lt;LinearLayout
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001358 android:orientation="vertical"
1359 android:layout_width="match_parent"
George Mount4ba18202015-07-27 12:39:28 -07001360 android:layout_height="match_parent"&gt;
1361 &lt;TextView android:layout_width="wrap_content"
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001362 android:layout_height="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -07001363 android:text="&commat;{user.firstName}"
1364 android:id="&commat;+id/firstName"/&gt;
1365 &lt;TextView android:layout_width="wrap_content"
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001366 android:layout_height="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -07001367 android:text="&commat;{user.lastName}"
1368 android:id="&commat;+id/lastName"/&gt;
1369 &lt;/LinearLayout&gt;
1370&lt;/layout&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001371</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001372<p>
1373 Will generate a binding class with:
1374</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001375
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001376<pre>
George Mount4ba18202015-07-27 12:39:28 -07001377public final TextView firstName;
1378public final TextView lastName;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001379</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001380<p>
1381 IDs are not nearly as necessary as without data binding, but there are still
1382 some instances where access to Views are still necessary from code.
1383</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001384
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001385<h3 id="variables2">
1386 Variables
1387</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001388
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001389<p>
1390 Each variable will be given accessor methods.
1391</p>
1392
1393<pre>
George Mount4ba18202015-07-27 12:39:28 -07001394&lt;data&gt;
1395 &lt;import type="android.graphics.drawable.Drawable"/&gt;
1396 &lt;variable name="user" type="com.example.User"/&gt;
1397 &lt;variable name="image" type="Drawable"/&gt;
1398 &lt;variable name="note" type="String"/&gt;
1399&lt;/data&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001400</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001401<p>
1402 will generate setters and getters in the binding:
1403</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001404
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001405<pre>
George Mount4ba18202015-07-27 12:39:28 -07001406public abstract com.example.User getUser();
1407public abstract void setUser(com.example.User user);
1408public abstract Drawable getImage();
1409public abstract void setImage(Drawable image);
1410public abstract String getNote();
1411public abstract void setNote(String note);
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001412</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001413<h3 id="viewstubs">
1414 ViewStubs
1415</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001416
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001417<p>
George Mount4ba18202015-07-27 12:39:28 -07001418 {@link android.view.ViewStub}s are a little different from normal Views. They start off invisible
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001419 and when they either are made visible or are explicitly told to inflate, they
1420 replace themselves in the layout by inflating another layout.
1421</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001422
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001423<p>
George Mount4ba18202015-07-27 12:39:28 -07001424 Because the <code>ViewStub</code> essentially disappears from the View hierarchy, the View
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001425 in the binding object must also disappear to allow collection. Because the
George Mount4ba18202015-07-27 12:39:28 -07001426 Views are final, a {@link android.databinding.ViewStubProxy} object takes the place of the
1427 <code>ViewStub</code>, giving the developer access to the ViewStub when it exists and also
1428 access to the inflated View hierarchy when the <code>ViewStub</code> has been inflated.
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001429</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001430
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001431<p>
1432 When inflating another layout, a binding must be established for the new
Mark Lu011c07b2016-07-28 11:15:11 -07001433 layout. Therefore, the <code>ViewStubProxy</code> must listen to the <code>ViewStub</code>'s
George Mount4ba18202015-07-27 12:39:28 -07001434 {@link android.view.ViewStub.OnInflateListener} and establish the binding at that time. Since
1435 only one can exist, the <code>ViewStubProxy</code> allows the developer to set an
1436 <code>OnInflateListener</code> on it that it will call after establishing the binding.
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001437</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001438
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001439<h3 id="advanced_binding">
1440 Advanced Binding
1441</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001442
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001443<h4 id="dynamic_variables">
1444 Dynamic Variables
1445</h4>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001446
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001447<p>
Mark Lu011c07b2016-07-28 11:15:11 -07001448 At times, the specific binding class won't be known. For example, a
George Mount4ba18202015-07-27 12:39:28 -07001449 {@link android.support.v7.widget.RecyclerView.Adapter} operating against arbitrary layouts
Mark Lu011c07b2016-07-28 11:15:11 -07001450 won't know the specific binding class. It still must assign the binding value during the
George Mount4ba18202015-07-27 12:39:28 -07001451 {@link android.support.v7.widget.RecyclerView.Adapter#onBindViewHolder}.
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001452</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001453
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001454<p>
1455 In this example, all layouts that the RecyclerView binds to have an "item"
George Mount4ba18202015-07-27 12:39:28 -07001456 variable. The <code>BindingHolder</code> has a <code>getBinding</code> method returning the
1457 {@link android.databinding.ViewDataBinding} base.
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001458</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001459
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001460<pre>
George Mount4ba18202015-07-27 12:39:28 -07001461public void onBindViewHolder(BindingHolder holder, int position) {
1462 final T item = mItems.get(position);
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001463 holder.getBinding().setVariable(BR.item, item);
1464 holder.getBinding().executePendingBindings();
1465}
1466</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001467<h4 id="immediate_binding">
1468 Immediate Binding
1469</h4>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001470
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001471<p>
1472 When a variable or observable changes, the binding will be scheduled to
1473 change before the next frame. There are times, however, when binding must be
1474 executed immediately. To force execution, use the
George Mount4ba18202015-07-27 12:39:28 -07001475 {@link android.databinding.ViewDataBinding#executePendingBindings()} method.
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001476</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001477
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001478<h4>
1479 Background Thread
1480</h4>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001481
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001482<p>
1483 You can change your data model in a background thread as long as it is not a
1484 collection. Data binding will localize each variable / field while evaluating
1485 to avoid any concurrency issues.
1486</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001487
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001488<h2 id="attribute_setters">
1489 Attribute Setters
1490</h2>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001491
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001492<p>
1493 Whenever a bound value changes, the generated binding class must call a
1494 setter method on the View with the binding expression. The data binding
1495 framework has ways to customize which method to call to set the value.
1496</p>
1497
1498<h3 id="automatic_setters">
1499 Automatic Setters
1500</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001501For an attribute, data binding tries to find the method setAttribute. The
1502namespace for the attribute does not matter, only the attribute name itself.
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001503<p>
Mark Lu011c07b2016-07-28 11:15:11 -07001504 For example, an expression associated with TextView's attribute
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001505 <strong><code>android:text</code></strong> will look for a setText(String).
1506 If the expression returns an int, data binding will search for a setText(int)
1507 method. Be careful to have the expression return the correct type, casting if
1508 necessary. Note that data binding will work even if no attribute exists with
1509 the given name. You can then easily "create" attributes for any setter by
Mark Lu011c07b2016-07-28 11:15:11 -07001510 using data binding. For example, support DrawerLayout doesn't have any
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001511 attributes, but plenty of setters. You can use the automatic setters to use
1512 one of these.
1513</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001514
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001515<pre>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001516&lt;android.support.v4.widget.<strong>DrawerLayout
1517 android:layout_width="wrap_content"
1518 android:layout_height="wrap_content"
1519 app:scrimColor="&commat;{&commat;color/scrim}"
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001520 app:drawerListener="&commat;{fragment.drawerListener}"/&gt;</strong>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001521</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001522<h3 id="renamed_setters">
1523 Renamed Setters
1524</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001525
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001526<p>
Mark Lu011c07b2016-07-28 11:15:11 -07001527 Some attributes have setters that don't match by name. For these
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001528 methods, an attribute may be associated with the setter through
George Mount4ba18202015-07-27 12:39:28 -07001529 {@link android.databinding.BindingMethods} annotation. This must be associated with
1530 a class and contains {@link android.databinding.BindingMethod} annotations, one for
1531 each renamed method. For example, the <strong><code>android:tint</code></strong> attribute
1532 is really associated with {@link android.widget.ImageView#setImageTintList}, not
1533 <code>setTint</code>.
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001534</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001535
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001536<pre>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001537&commat;BindingMethods({
George Mount4ba18202015-07-27 12:39:28 -07001538 &commat;BindingMethod(type = "android.widget.ImageView",
1539 attribute = "android:tint",
1540 method = "setImageTintList"),
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001541})
1542</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001543<p>
1544 It is unlikely that developers will need to rename setters; the android
1545 framework attributes have already been implemented.
1546</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001547
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001548<h3 id="custom_setters">
1549 Custom Setters
1550</h3>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001551
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001552<p>
1553 Some attributes need custom binding logic. For example, there is no
1554 associated setter for the <strong><code>android:paddingLeft</code></strong>
1555 attribute. Instead, <code>setPadding(left, top, right, bottom)</code> exists.
George Mount4ba18202015-07-27 12:39:28 -07001556 A static binding adapter method with the {@link android.databinding.BindingAdapter}
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001557 annotation allows the developer to customize how a setter for an attribute is
1558 called.
1559</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001560
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001561<p>
1562 The android attributes have already had <code>BindingAdapter</code>s created.
1563 For example, here is the one for <code>paddingLeft</code>:
1564</p>
1565
1566<pre>
George Mount4ba18202015-07-27 12:39:28 -07001567&commat;BindingAdapter("android:paddingLeft")
1568public static void setPaddingLeft(View view, int padding) {
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001569 view.setPadding(padding,
1570 view.getPaddingTop(),
1571 view.getPaddingRight(),
1572 view.getPaddingBottom());
1573}
1574</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001575<p>
1576 Binding adapters are useful for other types of customization. For example, a
1577 custom loader can be called off-thread to load an image.
1578</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001579
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001580<p>
1581 Developer-created binding adapters will override the data binding default
1582 adapters when there is a conflict.
1583</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001584
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001585<p>
1586 You can also have adapters that receive multiple parameters.
1587</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001588
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001589<pre>
George Mount4ba18202015-07-27 12:39:28 -07001590&commat;BindingAdapter({"bind:imageUrl", "bind:error"})
1591public static void loadImage(ImageView view, String url, Drawable error) {
1592 Picasso.with(view.getContext()).load(url).error(error).into(view);
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001593}
1594</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001595<pre>
Mark Lu011c07b2016-07-28 11:15:11 -07001596&lt;ImageView app:imageUrl="&commat;{venue.imageUrl}"
1597app:error="&commat;{&commat;drawable/venueError}"/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001598</pre>
1599
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001600<p>
1601 This adapter will be called if both <strong>imageUrl</strong> and
1602 <strong>error</strong> are used for an ImageView and <em>imageUrl</em> is a
1603 string and <em>error</em> is a drawable.
1604</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001605
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001606<ul>
1607 <li>Custom namespaces are ignored during matching.
1608 </li>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001609
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001610 <li>You can also write adapters for android namespace.
1611 </li>
1612</ul>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001613
George Mount4ba18202015-07-27 12:39:28 -07001614<p>
1615 Binding adapter methods may optionally take the old values in their handlers. A method
1616 taking old and new values should have all old values for the attributes come first, followed
1617 by the new values:
1618</p>
1619<pre>
1620&commat;BindingAdapter("android:paddingLeft")
1621public static void setPaddingLeft(View view, int oldPadding, int newPadding) {
1622 if (oldPadding != newPadding) {
1623 view.setPadding(newPadding,
1624 view.getPaddingTop(),
1625 view.getPaddingRight(),
1626 view.getPaddingBottom());
1627 }
1628}
1629</pre>
1630<p>
1631 Event handlers may only be used with interfaces or abstract classes with one abstract method.
1632 For example:
1633</p>
1634<pre>
1635&commat;BindingAdapter("android:onLayoutChange")
1636public static void setOnLayoutChangeListener(View view, View.OnLayoutChangeListener oldValue,
1637 View.OnLayoutChangeListener newValue) {
1638 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
1639 if (oldValue != null) {
1640 view.removeOnLayoutChangeListener(oldValue);
1641 }
1642 if (newValue != null) {
1643 view.addOnLayoutChangeListener(newValue);
1644 }
1645 }
1646}
1647</pre>
1648<p>
1649 When a listener has multiple methods, it must be split into multiple listeners. For example,
1650 {@link android.view.View.OnAttachStateChangeListener} has two methods:
1651 {@link android.view.View.OnAttachStateChangeListener#onViewAttachedToWindow onViewAttachedToWindow()} and
1652 {@link android.view.View.OnAttachStateChangeListener#onViewDetachedFromWindow onViewDetachedFromWindow()}.
1653 We must then create two interfaces to differentiate the attributes and handlers for them.
1654</p>
1655
1656<pre>
1657&commat;TargetApi(VERSION_CODES.HONEYCOMB_MR1)
1658public interface OnViewDetachedFromWindow {
1659 void onViewDetachedFromWindow(View v);
1660}
1661
1662&commat;TargetApi(VERSION_CODES.HONEYCOMB_MR1)
1663public interface OnViewAttachedToWindow {
1664 void onViewAttachedToWindow(View v);
1665}
1666</pre>
1667<p>
1668 Because changing one listener will also affect the other, we must have three different
1669 binding adapters, one for each attribute and one for both, should they both be set.
1670</p>
1671<pre>
1672&commat;BindingAdapter("android:onViewAttachedToWindow")
1673public static void setListener(View view, OnViewAttachedToWindow attached) {
1674 setListener(view, null, attached);
1675}
1676
1677&commat;BindingAdapter("android:onViewDetachedFromWindow")
1678public static void setListener(View view, OnViewDetachedFromWindow detached) {
1679 setListener(view, detached, null);
1680}
1681
1682&commat;BindingAdapter({"android:onViewDetachedFromWindow", "android:onViewAttachedToWindow"})
1683public static void setListener(View view, final OnViewDetachedFromWindow detach,
1684 final OnViewAttachedToWindow attach) {
1685 if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1) {
1686 final OnAttachStateChangeListener newListener;
1687 if (detach == null && attach == null) {
1688 newListener = null;
1689 } else {
1690 newListener = new OnAttachStateChangeListener() {
1691 &commat;Override
1692 public void onViewAttachedToWindow(View v) {
1693 if (attach != null) {
1694 attach.onViewAttachedToWindow(v);
1695 }
1696 }
1697
1698 &commat;Override
1699 public void onViewDetachedFromWindow(View v) {
1700 if (detach != null) {
1701 detach.onViewDetachedFromWindow(v);
1702 }
1703 }
1704 };
1705 }
1706 final OnAttachStateChangeListener oldListener = ListenerUtil.trackListener(view,
1707 newListener, R.id.onAttachStateChangeListener);
1708 if (oldListener != null) {
1709 view.removeOnAttachStateChangeListener(oldListener);
1710 }
1711 if (newListener != null) {
1712 view.addOnAttachStateChangeListener(newListener);
1713 }
1714 }
1715}
1716</pre>
1717<p>
1718 The above example is slightly more complicated than normal because View uses add and remove
1719 for the listener instead of a set method for {@link android.view.View.OnAttachStateChangeListener}.
1720 The <code>android.databinding.adapters.ListenerUtil</code> class helps keep track of the previous
1721 listeners so that they may be removed in the Binding Adaper.
1722</p>
1723<p>
1724 By annotating the interfaces <code>OnViewDetachedFromWindow</code> and
1725 <code>OnViewAttachedToWindow</code> with
1726 <code>&commat;TargetApi(VERSION_CODES.HONEYCOMB_MR1)</code>, the data binding code
1727 generator knows that the listener should only be generated when running on Honeycomb MR1
1728 and new devices, the same version supported by
1729 {@link android.view.View#addOnAttachStateChangeListener}.
1730</p>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001731<h2 id="converters">
1732 Converters
1733</h2>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001734
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001735<h3 id="object_conversions">
1736 Object Conversions
1737</h3>
1738
1739<p>
1740 When an Object is returned from a binding expression, a setter will be chosen
1741 from the automatic, renamed, and custom setters. The Object will be cast to a
1742 parameter type of the chosen setter.
1743</p>
1744
1745<p>
1746 This is a convenience for those using ObservableMaps to hold data. for
1747 example:
1748</p>
1749
1750<pre>
George Mount4ba18202015-07-27 12:39:28 -07001751&lt;TextView
Mark Lu011c07b2016-07-28 11:15:11 -07001752 android:text='&commat;{userMap["lastName"]}'
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001753 android:layout_width="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -07001754 android:layout_height="wrap_content"/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001755</pre>
1756
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001757<p>
1758The <code>userMap</code> returns an Object and that Object will be automatically cast to
1759 parameter type found in the setter <code>setText(CharSequence)</code>. When there
1760 may be confusion about the parameter type, the developer will need
1761 to cast in the expression.
1762</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001763
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001764<h3 id="custom_conversions">Custom Conversions</h3>
1765
1766<p>
1767 Sometimes conversions should be automatic between specific types. For
1768 example, when setting the background:
1769</p>
1770
1771<pre>
George Mount4ba18202015-07-27 12:39:28 -07001772&lt;View
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001773 android:background="&commat;{isError ? &commat;color/red : &commat;color/white}"
1774 android:layout_width="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -07001775 android:layout_height="wrap_content"/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001776</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001777<p>
1778 Here, the background takes a <code>Drawable</code>, but the color is an
1779 integer. Whenever a <code>Drawable</code> is expected and an integer is
1780 returned, the <code>int</code> should be converted to a
1781 <code>ColorDrawable</code>. This conversion is done using a static method
1782 with a BindingConversion annotation:
1783</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001784
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001785<pre>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001786&commat;BindingConversion
George Mount4ba18202015-07-27 12:39:28 -07001787public static ColorDrawable convertColorToDrawable(int color) {
1788 return new ColorDrawable(color);
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001789}
1790</pre>
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001791<p>
1792 Note that conversions only happen at the setter level, so it is <strong>not
1793 allowed</strong> to mix types like this:
1794</p>
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001795
Dirk Doughertyea1f6fb2015-05-26 20:31:12 -07001796<pre>
George Mount4ba18202015-07-27 12:39:28 -07001797&lt;View
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001798 android:background="&commat;{isError ? &commat;drawable/error : &commat;color/white}"
1799 android:layout_width="wrap_content"
George Mount4ba18202015-07-27 12:39:28 -07001800 android:layout_height="wrap_content"/&gt;
Dirk Dougherty493ff3c2015-05-26 16:52:23 -07001801</pre>
Daniel Yudaa41bb2015-12-08 10:58:07 -08001802
1803<h3 id="studio_support">Android Studio Support for Data Binding</h3>
1804
1805<p>
Cheryl Potter8dd7eea2016-04-04 17:10:06 -07001806Android Studio supports many of the code editing features for data binding code.
1807For example, it supports the following features for data binding expressions:</p>
1808<ul>
1809<li>Syntax highlighting</li>
1810<li>Flagging of expression language syntax errors</li>
1811<li>XML code completion</li>
1812<li>References, including <a class="external-link"
1813href="https://www.jetbrains.com/help/idea/2016.1/navigation-in-source-code.html?origin=old_help">
1814navigation</a> (such as navigate to a declaration) and
1815<a class="external-link"
1816href="https://www.jetbrains.com/help/idea/2016.1/viewing-inline-documentation.html?origin=old_help">
1817quick documentation</a></li>
1818</ul>
1819
1820<p class="note"><strong>Note:</strong> Arrays and a
1821<a href="https://docs.oracle.com/javase/tutorial/java/generics/types.html"
1822class="external-link">generic type</a>, such as the {@link
1823android.databinding.Observable} class, might display
1824errors when there are no errors.</p>
1825
Daniel Yudaa41bb2015-12-08 10:58:07 -08001826<p>
Cheryl Potter8dd7eea2016-04-04 17:10:06 -07001827The Preview pane displays default values for data binding expressions if
1828provided. In the following
1829example excerpt of an element from a layout XML file, the Preview pane displays
1830the {@code PLACEHOLDER} default text value in the <code>TextView</code>.
Daniel Yudaa41bb2015-12-08 10:58:07 -08001831</p>
1832
1833<pre>
1834&lt;TextView android:layout_width="wrap_content"
1835 android:layout_height="wrap_content"
1836 android:text="&commat;{user.firstName, default=PLACEHOLDER}"/&gt;
1837</pre>
1838
1839<p>
Cheryl Potter8dd7eea2016-04-04 17:10:06 -07001840If you need to display a default value during the design phase of your project,
1841you can also use tools attributes instead of default expression values, as
1842described in
1843<a class="external-link"
1844href="http://tools.android.com/tips/layout-designtime-attributes">
Daniel Yudaa41bb2015-12-08 10:58:07 -08001845Designtime Layout Attributes</a>.
Cheryl Potter8dd7eea2016-04-04 17:10:06 -07001846</p>
1847