| Andrew Solovay | 7877f69 | 2016-04-26 16:00:59 -0700 | [diff] [blame] | 1 | page.title=Data Binding Library |
| 2 | page.metaDescription=The Data Binding Library enables you to write declarative layouts. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 3 | page.tags="databinding", "layouts" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 4 | @jd:body |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 5 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 6 | <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 Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 21 | <a href="#writing_expressions">Writing your first set of data binding |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 22 | expressions</a> |
| 23 | </li> |
| 24 | |
| 25 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 26 | <a href="#data_object">Data Object</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 27 | </li> |
| 28 | |
| 29 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 30 | <a href="#binding_data">Binding Data</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 31 | </li> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 32 | <li> |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 33 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 42 | </li> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 43 | </ol> |
| 44 | </li> |
| 45 | |
| 46 | <li> |
| 47 | <a href="#layout_details">Layout Details</a> |
| 48 | <ol> |
| 49 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 50 | <a href="#imports">Imports</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 51 | </li> |
| 52 | |
| 53 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 54 | <a href="#variables">Variables</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 55 | </li> |
| 56 | |
| 57 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 58 | <a href="#custom_binding_class_names">Custom Binding Class Names</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 59 | </li> |
| 60 | |
| 61 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 62 | <a href="#includes">Includes</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 63 | </li> |
| 64 | |
| 65 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 66 | <a href="#expression_language">Expression Language</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 67 | </li> |
| 68 | </ol> |
| 69 | </li> |
| 70 | |
| 71 | <li> |
| 72 | <a href="#data_objects">Data Objects</a> |
| 73 | <ol> |
| 74 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 75 | <a href="#observable_objects">Observable Objects</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 76 | </li> |
| 77 | |
| 78 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 79 | <a href="#observablefields">ObservableFields</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 80 | </li> |
| 81 | |
| 82 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 83 | <a href="#observable_collections">Observable Collections</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 84 | </li> |
| 85 | </ol> |
| 86 | </li> |
| 87 | |
| 88 | <li> |
| 89 | <a href="#generated_binding">Generated Binding</a> |
| 90 | <ol> |
| 91 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 92 | <a href="#creating">Creating</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 93 | </li> |
| 94 | |
| 95 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 96 | <a href="#views_with_ids">Views With IDs</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 97 | </li> |
| 98 | |
| 99 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 100 | <a href="#variables">Variables</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 101 | </li> |
| 102 | |
| 103 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 104 | <a href="#viewstubs">ViewStubs</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 105 | </li> |
| 106 | |
| 107 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 108 | <a href="#advanced_binding">Advanced Binding</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 109 | </li> |
| 110 | </ol> |
| 111 | </li> |
| 112 | |
| 113 | <li> |
| 114 | <a href="#attribute_setters">Attribute Setters</a> |
| 115 | <ol> |
| 116 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 117 | <a href="#automatic_setters">Automatic Setters</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 118 | </li> |
| 119 | |
| 120 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 121 | <a href="#renamed_setters">Renamed Setters</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 122 | </li> |
| 123 | |
| 124 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 125 | <a href="#custom_setters">Custom Setters</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 126 | </li> |
| 127 | </ol> |
| 128 | </li> |
| 129 | |
| 130 | <li> |
| 131 | <a href="#converters">Converters</a> |
| 132 | <ol> |
| 133 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 134 | <a href="#object_conversions">Object Conversions</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 135 | </li> |
| 136 | |
| 137 | <li> |
| Dirk Dougherty | 518651c | 2015-05-27 20:24:37 -0700 | [diff] [blame] | 138 | <a href="#custom_conversions">Custom Conversions</a> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 139 | </li> |
| 140 | </ol> |
| 141 | </li> |
| Daniel Yu | daa41bb | 2015-12-08 10:58:07 -0800 | [diff] [blame] | 142 | |
| 143 | <li> |
| 144 | <a href="#studio_support">Android Studio Support for Data Binding</a> |
| 145 | </li> |
| 146 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 147 | </ol> |
| 148 | </div><!-- qv --> |
| 149 | </div><!-- qv-wrapper --> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 150 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 151 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 156 | |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 157 | <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 Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 162 | |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 163 | <p> |
| 164 | To use data binding, Android Plugin for Gradle <strong>1.5.0-alpha1</strong> |
| smain@google.com | 3d5a4c9 | 2016-09-12 12:07:26 -0700 | [diff] [blame] | 165 | or higher is required. See how to <a |
| 166 | href="/studio/releases/gradle-plugin.html#updating-plugin">update the Android |
| 167 | Plugin for Gradle</a>. |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 168 | </p> |
| Dirk Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 169 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 170 | <h2 id="build_environment"> |
| 171 | Build Environment |
| 172 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 173 | |
| sreevanis | 1a1d5db | 2015-10-30 14:44:51 -0700 | [diff] [blame] | 174 | <p> |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 175 | To get started with Data Binding, download the library from the Support |
| 176 | repository in the Android SDK manager. |
| sreevanis | 0cd6203 | 2015-11-03 09:57:57 -0800 | [diff] [blame] | 177 | </p> |
| 178 | |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 179 | <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> |
| sreevanis | 1a1d5db | 2015-10-30 14:44:51 -0700 | [diff] [blame] | 183 | |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 184 | <p> |
| 185 | Use the following code snippet to configure data binding: |
| 186 | </p> |
| sreevanis | 1a1d5db | 2015-10-30 14:44:51 -0700 | [diff] [blame] | 187 | <pre> |
| 188 | android { |
| 189 | .... |
| 190 | dataBinding { |
| 191 | enabled = true |
| 192 | } |
| 193 | } |
| 194 | </pre> |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 195 | <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 Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 200 | |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 201 | <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 Yu | daa41bb | 2015-12-08 10:58:07 -0800 | [diff] [blame] | 206 | </p> |
| Dirk Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 207 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 208 | <h2 id="data_binding_layout_files"> |
| 209 | Data Binding Layout Files |
| 210 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 211 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 212 | <h3 id="writing_expressions"> |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 213 | Writing your first set of data binding expressions |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 214 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 215 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 216 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 222 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 223 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 224 | <?xml version="1.0" encoding="utf-8"?> |
| 225 | <layout xmlns:android="http://schemas.android.com/apk/res/android"> |
| 226 | <data> |
| 227 | <variable name="user" type="com.example.User"/> |
| 228 | </data> |
| 229 | <LinearLayout |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 230 | android:orientation="vertical" |
| 231 | android:layout_width="match_parent" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 232 | android:layout_height="match_parent"> |
| 233 | <TextView android:layout_width="wrap_content" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 234 | android:layout_height="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 235 | android:text="@{user.firstName}"/> |
| 236 | <TextView android:layout_width="wrap_content" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 237 | android:layout_height="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 238 | android:text="@{user.lastName}"/> |
| 239 | </LinearLayout> |
| 240 | </layout> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 241 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 242 | <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 Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 246 | <pre> |
| 247 | <<strong>variable name="user" type="com.example.User"</strong>/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 248 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 249 | <p> |
| 250 | Expressions within the layout are written in the attribute properties using |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 251 | the "<code>@{}</code>" syntax. Here, the TextView's text is set to |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 252 | the firstName property of user: |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 253 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 254 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 255 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 256 | <TextView android:layout_width="wrap_content" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 257 | android:layout_height="wrap_content" |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 258 | android:text="@{user.firstName}"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 259 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 260 | <h3 id="data_object"> |
| 261 | Data Object |
| 262 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 263 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 264 | <p> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 265 | Let's assume for now that you have a plain-old Java object (POJO) for User: |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 266 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 267 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 268 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 269 | public 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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 278 | <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 Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 283 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 284 | public 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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 290 | } |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 291 | public String getFirstName() { |
| 292 | return this.firstName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 293 | } |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 294 | public String getLastName() { |
| 295 | return this.lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 299 | <p> |
| 300 | From the perspective of data binding, these two classes are equivalent. The |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 301 | expression <strong><code>@{user.firstName}</code></strong> used |
| 302 | for the TextView's <strong><code>android:text</code></strong> attribute will |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 303 | access the <strong><code>firstName</code></strong> field in the former class |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 304 | 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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 307 | </p> |
| 308 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 309 | <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 Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 315 | file, converting it to Pascal case and suffixing "Binding" to it. The above |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 316 | 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 Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 318 | layout properties (e.g. the <code>user</code> variable) to the layout's Views |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 319 | 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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 324 | @Override |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 325 | protected 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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 329 | binding.setUser(user); |
| 330 | } |
| 331 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 332 | <p> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 333 | You're done! Run the application and you'll see Test User in the UI. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 334 | Alternatively, you can get the view via: |
| 335 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 336 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 337 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 338 | MainActivityBinding binding = MainActivityBinding.<em>inflate</em>(getLayoutInflater()); |
| 339 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 340 | <p> |
| 341 | If you are using data binding items inside a ListView or RecyclerView |
| 342 | adapter, you may prefer to use: |
| 343 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 344 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 345 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 346 | ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 347 | //or |
| 348 | ListItemBinding binding = DataBindingUtil.<em>inflate</em>(layoutInflater, R.layout.<em><strong>list_item</strong></em>, viewGroup, <strong>false</strong>); |
| 349 | </pre> |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 350 | <h3 id="event_handling">Event Handling</h3> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 351 | <p> |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 352 | Data Binding allows you to write expressions handling events that are dispatched from the views (e.g. onClick). |
| 353 | Event attribute names are governed by the name of the listener method with a few exceptions. |
| 354 | For example, {@link android.view.View.OnLongClickListener} has a method {@link android.view.View.OnLongClickListener#onLongClick onLongClick()}, |
| 355 | so the attribute for this event is <code>android:onLongClick</code>. |
| 356 | There 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. |
| 364 | Data 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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 382 | </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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 389 | } |
| 390 | </pre> |
| 391 | <p> |
| 392 | The binding expression can assign the click listener for a View: |
| 393 | </p> |
| 394 | <pre> |
| 395 | <?xml version="1.0" encoding="utf-8"?> |
| 396 | <layout xmlns:android="http://schemas.android.com/apk/res/android"> |
| 397 | <data> |
| 398 | <variable name="handlers" type="com.example.Handlers"/> |
| 399 | <variable name="user" type="com.example.User"/> |
| 400 | </data> |
| 401 | <LinearLayout |
| 402 | android:orientation="vertical" |
| 403 | android:layout_width="match_parent" |
| 404 | android:layout_height="match_parent"> |
| 405 | <TextView android:layout_width="wrap_content" |
| 406 | android:layout_height="wrap_content" |
| 407 | android:text="@{user.firstName}" |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 408 | android:onClick="@{handlers::onClickFriend}"/> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 409 | </LinearLayout> |
| 410 | </layout> |
| 411 | </pre> |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 412 | <p> |
| 413 | Note that the signature of the method in the expression must exactly match the signature of the method in the |
| 414 | Listener 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> |
| 433 | public 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 Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 439 | <?xml version="1.0" encoding="utf-8"?> |
| 440 | <layout xmlns:android="http://schemas.android.com/apk/res/android"> |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 441 | <data> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 442 | <variable name="task" type="com.android.example.Task" /> |
| 443 | <variable name="presenter" type="com.android.example.Presenter" /> |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 444 | </data> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 445 | <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> |
| 446 | <Button android:layout_width="wrap_content" android:layout_height="wrap_content" |
| 447 | android:onClick="@{() -> presenter.onSaveClick(task)}" /> |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 448 | </LinearLayout> |
| 449 | </layout> |
| 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 Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 470 | android:onClick="@{(view) -> presenter.onSaveClick(task)}" |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 471 | </pre> |
| 472 | Or if you wanted to use the parameter in the expression, it could work as follows: |
| 473 | <pre> |
| 474 | public class Presenter { |
| 475 | public void onSaveClick(View view, Task task){} |
| 476 | } |
| 477 | </pre> |
| 478 | <pre> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 479 | android:onClick="@{(theView) -> presenter.onSaveClick(theView, task)}" |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 480 | </pre> |
| 481 | You can use a lambda expression with more than one parameter: |
| 482 | <pre> |
| 483 | public class Presenter { |
| 484 | public void onCompletedChanged(Task task, boolean completed){} |
| 485 | } |
| 486 | </pre> |
| 487 | <pre> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 488 | <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" |
| 489 | android:onCheckedChanged="@{(cb, isChecked) -> presenter.completeChanged(task, isChecked)}" /> |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 490 | </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> |
| 498 | public class Presenter { |
| 499 | public boolean onLongClick(View view, Task task){} |
| 500 | } |
| 501 | </pre> |
| 502 | <pre> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 503 | android:onLongClick="@{(theView) -> presenter.onLongClick(theView, task)}" |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 504 | </pre> |
| 505 | <p> |
| 506 | If the expression cannot be evaluated due to {@code null} objects, Data Binding returns |
| 507 | the 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> |
| 511 | If 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 Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 515 | android:onClick="@{(v) -> v.isVisible() ? doSomething() : void}" |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 516 | </pre> |
| 517 | |
| 518 | <h5>Avoid Complex Listeners</h5> |
| 519 | Listener expressions are very powerful and can make your code very easy to read. |
| Mark Lu | c4a0139 | 2016-07-18 10:42:11 -0700 | [diff] [blame] | 520 | On the other hand, listeners containing complex expressions make your layouts hard to read and unmaintainable. |
| 521 | These expressions should be as simple as passing available data from your UI to your callback method. You should implement |
| Yigit Boyar | 05eaab6 | 2016-05-25 10:45:01 -0700 | [diff] [blame] | 522 | any business logic inside the callback method that you invoked from the listener expression. |
| George Mount | 9b332f3 | 2016-01-28 14:31:25 -0800 | [diff] [blame] | 523 | |
| 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 Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 553 | <h2 id="layout_details"> |
| 554 | Layout Details |
| 555 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 556 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 557 | <h3 id="imports"> |
| 558 | Imports |
| 559 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 560 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 561 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 566 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 567 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 568 | <data> |
| 569 | <import type="android.view.View"/> |
| 570 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 571 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 572 | <p> |
| 573 | Now, View may be used within your binding expression: |
| 574 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 575 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 576 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 577 | <TextView |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 578 | android:text="@{user.lastName}" |
| 579 | android:layout_width="wrap_content" |
| 580 | android:layout_height="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 581 | android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 582 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 583 | <p> |
| 584 | When there are class name conflicts, one of the classes may be renamed to an |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 585 | "alias:" |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 586 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 587 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 588 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 589 | <import type="android.view.View"/> |
| 590 | <import type="com.example.real.estate.View" |
| 591 | alias="Vista"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 592 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 593 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 600 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 601 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 602 | <data> |
| 603 | <import type="com.example.User"/> |
| 604 | <import type="java.util.List"/> |
| 605 | <variable name="user" type="User"/> |
| Mark Lu | b1b5efd | 2016-08-04 14:45:42 -0700 | [diff] [blame] | 606 | <variable name="userList" type="List&lt;User&gt;"/> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 607 | </data> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 608 | </pre> |
| Dirk Dougherty | 08710f1 | 2015-05-28 22:01:36 -0700 | [diff] [blame] | 609 | <p class="caution"> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 610 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 617 | <TextView |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 618 | android:text="@{((User)(user.connection)).lastName}" |
| 619 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 620 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 621 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 622 | <p> |
| 623 | Imported types may also be used when referencing static fields and methods in |
| 624 | expressions: |
| 625 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 626 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 627 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 628 | <data> |
| 629 | <import type="com.example.MyStringUtils"/> |
| 630 | <variable name="user" type="com.example.User"/> |
| 631 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 632 | … |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 633 | <TextView |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 634 | android:text="@{MyStringUtils.capitalize(user.lastName)}" |
| 635 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 636 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 637 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 638 | <p> |
| 639 | Just as in Java, <code>java.lang.*</code> is imported automatically. |
| 640 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 641 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 642 | <h3 id="variables"> |
| 643 | Variables |
| 644 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 645 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 646 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 655 | <data> |
| 656 | <import type="android.graphics.drawable.Drawable"/> |
| 657 | <variable name="user" type="com.example.User"/> |
| 658 | <variable name="image" type="Drawable"/> |
| 659 | <variable name="note" type="String"/> |
| 660 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 661 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 662 | <p> |
| 663 | The variable types are inspected at compile time, so if a variable implements |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 664 | {@link android.databinding.Observable} or is an <a href= |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 665 | "#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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 670 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 671 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 676 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 677 | <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 — <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 684 | |
| George Mount | 9b332f3 | 2016-01-28 14:31:25 -0800 | [diff] [blame] | 685 | <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 Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 693 | <h3 id="custom_binding_class_names"> |
| 694 | Custom Binding Class Names |
| 695 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 696 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 697 | <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 Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 700 | capitalizing the following letter and then suffixing "Binding". This class |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 701 | 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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 707 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 708 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 713 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 714 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 715 | <data class="ContactItem"> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 716 | ... |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 717 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 718 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 719 | <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 Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 723 | ".": |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 724 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 725 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 726 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 727 | <data class=".ContactItem"> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 728 | ... |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 729 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 730 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 731 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 735 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 736 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 737 | <data class="com.example.ContactItem"> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 738 | ... |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 739 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 740 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 741 | <h3 id="includes"> |
| 742 | Includes |
| 743 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 744 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 745 | <p> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 746 | Variables may be passed into an included layout's binding from the |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 747 | containing layout by using the application namespace and the variable name in |
| 748 | an attribute: |
| 749 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 750 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 751 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 752 | <?xml version="1.0" encoding="utf-8"?> |
| 753 | <layout xmlns:android="http://schemas.android.com/apk/res/android" |
| 754 | xmlns:bind="http://schemas.android.com/apk/res-auto"> |
| 755 | <data> |
| 756 | <variable name="user" type="com.example.User"/> |
| 757 | </data> |
| 758 | <LinearLayout |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 759 | android:orientation="vertical" |
| 760 | android:layout_width="match_parent" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 761 | android:layout_height="match_parent"> |
| 762 | <include layout="@layout/name" |
| 763 | bind:user="@{user}"/> |
| 764 | <include layout="@layout/contact" |
| 765 | bind:user="@{user}"/> |
| 766 | </LinearLayout> |
| 767 | </layout> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 768 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 769 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 773 | <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 | <?xml version="1.0" encoding="utf-8"?> |
| 779 | <layout xmlns:android="http://schemas.android.com/apk/res/android" |
| 780 | xmlns:bind="http://schemas.android.com/apk/res-auto"> |
| 781 | <data> |
| 782 | <variable name="user" type="com.example.User"/> |
| 783 | </data> |
| 784 | <merge> |
| 785 | <include layout="@layout/name" |
| 786 | bind:user="@{user}"/> |
| 787 | <include layout="@layout/contact" |
| 788 | bind:user="@{user}"/> |
| 789 | </merge> |
| 790 | </layout> |
| 791 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 792 | <h3 id="expression_language"> |
| 793 | Expression Language |
| 794 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 795 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 796 | <h4 id="common_features"> |
| 797 | Common Features |
| 798 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 799 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 800 | <p> |
| 801 | The expression language looks a lot like a Java expression. These are the |
| 802 | same: |
| 803 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 804 | |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 805 | <ul> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 806 | <li>Mathematical <strong><code>+ - / * %</code></strong> |
| 807 | </li> |
| 808 | |
| 809 | <li>String concatenation <strong><code>+</code></strong> |
| 810 | </li> |
| 811 | |
| 812 | <li> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 813 | Logical <strong><code>&& ||</code></strong> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 814 | </li> |
| 815 | |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 816 | <li>Binary <strong><code>& | ^</code></strong> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 817 | </li> |
| 818 | |
| 819 | <li>Unary <strong><code>+ - ! ~</code></strong> |
| 820 | </li> |
| 821 | |
| 822 | <li>Shift <strong><code>>> >>> <<</code></strong> |
| 823 | </li> |
| 824 | |
| 825 | <li>Comparison <strong><code>== > < >= <=</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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 852 | </ul> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 853 | |
| 854 | <p> |
| 855 | Examples: |
| 856 | </p> |
| 857 | |
| 858 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 859 | android:text="@{String.valueOf(index + 1)}" |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 860 | android:visibility="@{age < 13 ? View.GONE : View.VISIBLE}" |
| 861 | android:transitionName='@{"image_" + id}' |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 862 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 863 | <h4 id="missing_operations"> |
| 864 | Missing Operations |
| 865 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 866 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 867 | <p> |
| 868 | A few operations are missing from the expression syntax that you can use in |
| 869 | Java. |
| 870 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 871 | |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 872 | <ul> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 873 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 887 | </ul> |
| 888 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 889 | <h4 id="null_coalescing_operator"> |
| 890 | Null Coalescing Operator |
| 891 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 892 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 893 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 899 | <strong>android:text="@{user.displayName ?? user.lastName}"</strong> |
| 900 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 901 | <p> |
| 902 | This is functionally equivalent to: |
| 903 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 904 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 905 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 906 | <strong>android:text="@{user.displayName != null ? user.displayName : user.lastName}"</strong> |
| 907 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 908 | <h4 id="property_reference"> |
| 909 | Property Reference |
| 910 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 911 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 912 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 919 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 920 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 921 | <strong>android:text="@{user.lastName}"</strong> |
| 922 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 923 | <h4> |
| 924 | Avoiding NullPointerException |
| 925 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 926 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 927 | <p> |
| 928 | Generated data binding code automatically checks for nulls and avoid null |
| 929 | pointer exceptions. For example, in the expression |
| 930 | <code>@{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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 935 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 936 | <h4 id="collections"> |
| 937 | Collections |
| 938 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 939 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 940 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 946 | <data> |
| 947 | <import type="android.util.SparseArray"/> |
| 948 | <import type="java.util.Map"/> |
| 949 | <import type="java.util.List"/> |
| Mark Lu | b1b5efd | 2016-08-04 14:45:42 -0700 | [diff] [blame] | 950 | <variable name="list" type="List&lt;String&gt;"/> |
| 951 | <variable name="sparse" type="SparseArray&lt;String&gt;"/> |
| 952 | <variable name="map" type="Map&lt;String, String&gt;"/> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 953 | <variable name="index" type="int"/> |
| 954 | <variable name="key" type="String"/> |
| 955 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 956 | … |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 957 | android:text="@{list[index]}" |
| 958 | … |
| 959 | android:text="@{sparse[index]}" |
| 960 | … |
| 961 | android:text="@{map[key]}" |
| 962 | |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 963 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 964 | <h4 id="string_literals"> |
| 965 | String Literals |
| 966 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 967 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 968 | <p> |
| 969 | When using single quotes around the attribute value, it is easy to use double |
| 970 | quotes in the expression: |
| 971 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 972 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 973 | <pre> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 974 | android:text='@{map["firstName"]}' |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 975 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 976 | <p> |
| 977 | It is also possible to use double quotes to surround the attribute value. |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 978 | When doing so, String literals should either use the ' or back quote |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 979 | (`). |
| 980 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 981 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 982 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 983 | android:text="@{map[`firstName`}" |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 984 | android:text="@{map['firstName']}" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 985 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 986 | <h4 id="resources"> |
| 987 | Resources |
| 988 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 989 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 990 | <p> |
| 991 | It is possible to access resources as part of expressions using the normal |
| 992 | syntax: |
| 993 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 994 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 995 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 996 | android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 997 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 998 | <p> |
| 999 | Format strings and plurals may be evaluated by providing parameters: |
| 1000 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1001 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1002 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1003 | android:text="@{@string/nameFormat(firstName, lastName)}" |
| 1004 | android:text="@{@plurals/banana(bananaCount)}" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1005 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1006 | <p> |
| 1007 | When a plural takes multiple parameters, all parameters should be passed: |
| 1008 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1009 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1010 | <pre> |
| 1011 | |
| 1012 | Have an orange |
| 1013 | Have %d oranges |
| 1014 | |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1015 | android:text="@{@plurals/orange(orangeCount, orangeCount)}" |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1016 | </pre> |
| 1017 | <p> |
| 1018 | Some resources require explicit type evaluation. |
| 1019 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1020 | |
| 1021 | <table> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1022 | <tr> |
| 1023 | <th> |
| 1024 | Type |
| 1025 | </th> |
| 1026 | <th> |
| 1027 | Normal Reference |
| 1028 | </th> |
| 1029 | <th> |
| 1030 | Expression Reference |
| 1031 | </th> |
| 1032 | </tr> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1033 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1034 | <tr> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1035 | <td> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1036 | String[] |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1037 | </td> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1038 | <td> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1039 | @array |
| 1040 | </td> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1041 | <td> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1042 | @stringArray |
| 1043 | </td> |
| 1044 | </tr> |
| 1045 | |
| 1046 | <tr> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1047 | <td> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1048 | int[] |
| 1049 | </td> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1050 | <td> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1051 | @array |
| 1052 | </td> |
| 1053 | <td> |
| 1054 | @intArray |
| 1055 | </td> |
| 1056 | </tr> |
| 1057 | |
| 1058 | <tr> |
| 1059 | <td> |
| 1060 | TypedArray |
| 1061 | </td> |
| 1062 | <td> |
| 1063 | @array |
| 1064 | </td> |
| 1065 | <td> |
| 1066 | @typedArray |
| 1067 | </td> |
| 1068 | </tr> |
| 1069 | |
| 1070 | <tr> |
| 1071 | <td> |
| 1072 | Animator |
| 1073 | </td> |
| 1074 | <td> |
| 1075 | @animator |
| 1076 | </td> |
| 1077 | <td> |
| 1078 | @animator |
| 1079 | </td> |
| 1080 | </tr> |
| 1081 | |
| 1082 | <tr> |
| 1083 | <td> |
| 1084 | StateListAnimator |
| 1085 | </td> |
| 1086 | <td> |
| 1087 | @animator |
| 1088 | </td> |
| 1089 | <td> |
| 1090 | @stateListAnimator |
| 1091 | </td> |
| 1092 | </tr> |
| 1093 | |
| 1094 | <tr> |
| 1095 | <td> |
| 1096 | color <code>int</code> |
| 1097 | </td> |
| 1098 | <td> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1099 | @color |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1100 | </td> |
| 1101 | <td> |
| 1102 | @color |
| 1103 | </td> |
| 1104 | </tr> |
| 1105 | |
| 1106 | <tr> |
| 1107 | <td> |
| 1108 | ColorStateList |
| 1109 | </td> |
| 1110 | <td> |
| 1111 | @color |
| 1112 | </td> |
| 1113 | <td> |
| 1114 | @colorStateList |
| 1115 | </td> |
| 1116 | </tr> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1117 | </table> |
| 1118 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1119 | <h2 id="data_objects"> |
| 1120 | Data Objects |
| 1121 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1122 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1123 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1128 | <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 Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1131 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1132 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1133 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1137 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1138 | <h3 id="observable_objects"> |
| 1139 | Observable Objects |
| 1140 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1141 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1142 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1143 | A class implementing the {@link android.databinding.Observable} interface |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1144 | 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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1147 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1148 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1149 | The {@link android.databinding.Observable} interface has a mechanism to add and remove |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1150 | listeners, but notifying is up to the developer. To make development easier, |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1151 | a base class, {@link android.databinding.BaseObservable}, was created to implement the |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1152 | listener registration mechanism. The data class implementer is still |
| 1153 | responsible for notifying when the properties change. This is done by |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1154 | assigning a {@link android.databinding.Bindable} annotation to the getter and notifying in |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1155 | the setter. |
| 1156 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1157 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1158 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1159 | private static class User extends BaseObservable { |
| 1160 | private String firstName; |
| 1161 | private String lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1162 | @Bindable |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1163 | public String getFirstName() { |
| 1164 | return this.firstName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1165 | } |
| 1166 | @Bindable |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1167 | public String getLastName() { |
| 1168 | return this.lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1169 | } |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1170 | public void setFirstName(String firstName) { |
| 1171 | this.firstName = firstName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1172 | notifyPropertyChanged(BR.firstName); |
| 1173 | } |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1174 | public void setLastName(String lastName) { |
| 1175 | this.lastName = lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1176 | notifyPropertyChanged(BR.lastName); |
| 1177 | } |
| 1178 | } |
| 1179 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1180 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1181 | The {@link android.databinding.Bindable} annotation generates an entry in the BR class file |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1182 | during compilation. The BR class file will be generated in the module |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1183 | 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 Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1186 | efficiently. |
| 1187 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1188 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1189 | <h3 id="observablefields"> |
| 1190 | ObservableFields |
| 1191 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1192 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1193 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1194 | 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 Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1209 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1210 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1211 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1212 | private static class User { |
| 1213 | public final ObservableField<String> firstName = |
| 1214 | new ObservableField<>(); |
| 1215 | public final ObservableField<String> lastName = |
| 1216 | new ObservableField<>(); |
| 1217 | public final ObservableInt age = new ObservableInt(); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1218 | } |
| 1219 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1220 | <p> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1221 | That's it! To access the value, use the set and get accessor methods: |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1222 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1223 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1224 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1225 | user.firstName.set("Google"); |
| 1226 | int age = user.age.get(); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1227 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1228 | <h3 id="observable_collections"> |
| 1229 | Observable Collections |
| 1230 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1231 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1232 | <p> |
| 1233 | Some applications use more dynamic structures to hold data. Observable |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1234 | collections allow keyed access to these data objects. |
| 1235 | {@link android.databinding.ObservableArrayMap} is |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1236 | useful when the key is a reference type, such as String. |
| 1237 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1238 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1239 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1240 | ObservableArrayMap<String, Object> user = new ObservableArrayMap<>(); |
| 1241 | user.put("firstName", "Google"); |
| 1242 | user.put("lastName", "Inc."); |
| 1243 | user.put("age", 17); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1244 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1245 | <p> |
| 1246 | In the layout, the map may be accessed through the String keys: |
| 1247 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1248 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1249 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1250 | <data> |
| 1251 | <import type="android.databinding.ObservableMap"/> |
| Mark Lu | b1b5efd | 2016-08-04 14:45:42 -0700 | [diff] [blame] | 1252 | <variable name="user" type="ObservableMap&lt;String, Object&gt;"/> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1253 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1254 | … |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1255 | <TextView |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1256 | android:text='@{user["lastName"]}' |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1257 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1258 | android:layout_height="wrap_content"/> |
| 1259 | <TextView |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1260 | android:text='@{String.valueOf(1 + (Integer)user["age"])}' |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1261 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1262 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1263 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1264 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1265 | {@link android.databinding.ObservableArrayList} is useful when the key is an integer: |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1266 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1267 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1268 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1269 | ObservableArrayList<Object> user = new ObservableArrayList<>(); |
| 1270 | user.add("Google"); |
| 1271 | user.add("Inc."); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1272 | user.add(17); |
| 1273 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1274 | <p> |
| 1275 | In the layout, the list may be accessed through the indices: |
| 1276 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1277 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1278 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1279 | <data> |
| 1280 | <import type="android.databinding.ObservableList"/> |
| 1281 | <import type="com.example.my.app.Fields"/> |
| Mark Lu | b1b5efd | 2016-08-04 14:45:42 -0700 | [diff] [blame] | 1282 | <variable name="user" type="ObservableList&lt;Object&gt;"/> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1283 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1284 | … |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1285 | <TextView |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1286 | android:text='@{user[Fields.LAST_NAME]}' |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1287 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1288 | android:layout_height="wrap_content"/> |
| 1289 | <TextView |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1290 | android:text='@{String.valueOf(1 + (Integer)user[Fields.AGE])}' |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1291 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1292 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1293 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1294 | <h2 id="generated_binding"> |
| 1295 | Generated Binding |
| 1296 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1297 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1298 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1302 | classes all extend {@link android.databinding.ViewDataBinding}. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1303 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1304 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1305 | <h3 id="creating"> |
| 1306 | Creating |
| 1307 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1308 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1309 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1315 | version that only takes a {@link android.view.LayoutInflater} and one that takes a |
| 1316 | {@link android.view.ViewGroup} as well: |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1317 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1318 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1319 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1320 | MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater); |
| 1321 | MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater, viewGroup, false); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1322 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1323 | <p> |
| 1324 | If the layout was inflated using a different mechanism, it may be bound |
| 1325 | separately: |
| 1326 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1327 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1328 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1329 | MyLayoutBinding binding = MyLayoutBinding.bind(viewRoot); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1330 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1331 | <p> |
| 1332 | Sometimes the binding cannot be known in advance. In such cases, the binding |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1333 | can be created using the {@link android.databinding.DataBindingUtil} class: |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1334 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1335 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1336 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1337 | ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater, layoutId, |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1338 | parent, attachToParent); |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1339 | ViewDataBinding binding = DataBindingUtil.bindTo(viewRoot, layoutId); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1340 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1341 | <h3 id="views_with_ids"> |
| 1342 | Views With IDs |
| 1343 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1344 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1345 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1351 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1352 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1353 | <layout xmlns:android="http://schemas.android.com/apk/res/android"> |
| 1354 | <data> |
| 1355 | <variable name="user" type="com.example.User"/> |
| 1356 | </data> |
| 1357 | <LinearLayout |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1358 | android:orientation="vertical" |
| 1359 | android:layout_width="match_parent" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1360 | android:layout_height="match_parent"> |
| 1361 | <TextView android:layout_width="wrap_content" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1362 | android:layout_height="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1363 | android:text="@{user.firstName}" |
| 1364 | android:id="@+id/firstName"/> |
| 1365 | <TextView android:layout_width="wrap_content" |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1366 | android:layout_height="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1367 | android:text="@{user.lastName}" |
| 1368 | android:id="@+id/lastName"/> |
| 1369 | </LinearLayout> |
| 1370 | </layout> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1371 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1372 | <p> |
| 1373 | Will generate a binding class with: |
| 1374 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1375 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1376 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1377 | public final TextView firstName; |
| 1378 | public final TextView lastName; |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1379 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1380 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1384 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1385 | <h3 id="variables2"> |
| 1386 | Variables |
| 1387 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1388 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1389 | <p> |
| 1390 | Each variable will be given accessor methods. |
| 1391 | </p> |
| 1392 | |
| 1393 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1394 | <data> |
| 1395 | <import type="android.graphics.drawable.Drawable"/> |
| 1396 | <variable name="user" type="com.example.User"/> |
| 1397 | <variable name="image" type="Drawable"/> |
| 1398 | <variable name="note" type="String"/> |
| 1399 | </data> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1400 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1401 | <p> |
| 1402 | will generate setters and getters in the binding: |
| 1403 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1404 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1405 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1406 | public abstract com.example.User getUser(); |
| 1407 | public abstract void setUser(com.example.User user); |
| 1408 | public abstract Drawable getImage(); |
| 1409 | public abstract void setImage(Drawable image); |
| 1410 | public abstract String getNote(); |
| 1411 | public abstract void setNote(String note); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1412 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1413 | <h3 id="viewstubs"> |
| 1414 | ViewStubs |
| 1415 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1416 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1417 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1418 | {@link android.view.ViewStub}s are a little different from normal Views. They start off invisible |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1419 | 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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1422 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1423 | <p> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1424 | Because the <code>ViewStub</code> essentially disappears from the View hierarchy, the View |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1425 | in the binding object must also disappear to allow collection. Because the |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1426 | 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 Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1429 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1430 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1431 | <p> |
| 1432 | When inflating another layout, a binding must be established for the new |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1433 | layout. Therefore, the <code>ViewStubProxy</code> must listen to the <code>ViewStub</code>'s |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1434 | {@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 Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1437 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1438 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1439 | <h3 id="advanced_binding"> |
| 1440 | Advanced Binding |
| 1441 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1442 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1443 | <h4 id="dynamic_variables"> |
| 1444 | Dynamic Variables |
| 1445 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1446 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1447 | <p> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1448 | At times, the specific binding class won't be known. For example, a |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1449 | {@link android.support.v7.widget.RecyclerView.Adapter} operating against arbitrary layouts |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1450 | won't know the specific binding class. It still must assign the binding value during the |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1451 | {@link android.support.v7.widget.RecyclerView.Adapter#onBindViewHolder}. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1452 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1453 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1454 | <p> |
| 1455 | In this example, all layouts that the RecyclerView binds to have an "item" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1456 | variable. The <code>BindingHolder</code> has a <code>getBinding</code> method returning the |
| 1457 | {@link android.databinding.ViewDataBinding} base. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1458 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1459 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1460 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1461 | public void onBindViewHolder(BindingHolder holder, int position) { |
| 1462 | final T item = mItems.get(position); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1463 | holder.getBinding().setVariable(BR.item, item); |
| 1464 | holder.getBinding().executePendingBindings(); |
| 1465 | } |
| 1466 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1467 | <h4 id="immediate_binding"> |
| 1468 | Immediate Binding |
| 1469 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1470 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1471 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1475 | {@link android.databinding.ViewDataBinding#executePendingBindings()} method. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1476 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1477 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1478 | <h4> |
| 1479 | Background Thread |
| 1480 | </h4> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1481 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1482 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1487 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1488 | <h2 id="attribute_setters"> |
| 1489 | Attribute Setters |
| 1490 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1491 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1492 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1501 | For an attribute, data binding tries to find the method setAttribute. The |
| 1502 | namespace for the attribute does not matter, only the attribute name itself. |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1503 | <p> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1504 | For example, an expression associated with TextView's attribute |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1505 | <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 Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1510 | using data binding. For example, support DrawerLayout doesn't have any |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1511 | attributes, but plenty of setters. You can use the automatic setters to use |
| 1512 | one of these. |
| 1513 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1514 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1515 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1516 | <android.support.v4.widget.<strong>DrawerLayout |
| 1517 | android:layout_width="wrap_content" |
| 1518 | android:layout_height="wrap_content" |
| 1519 | app:scrimColor="@{@color/scrim}" |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1520 | app:drawerListener="@{fragment.drawerListener}"/></strong> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1521 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1522 | <h3 id="renamed_setters"> |
| 1523 | Renamed Setters |
| 1524 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1525 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1526 | <p> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1527 | Some attributes have setters that don't match by name. For these |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1528 | methods, an attribute may be associated with the setter through |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1529 | {@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 Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1534 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1535 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1536 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1537 | @BindingMethods({ |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1538 | @BindingMethod(type = "android.widget.ImageView", |
| 1539 | attribute = "android:tint", |
| 1540 | method = "setImageTintList"), |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1541 | }) |
| 1542 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1543 | <p> |
| 1544 | It is unlikely that developers will need to rename setters; the android |
| 1545 | framework attributes have already been implemented. |
| 1546 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1547 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1548 | <h3 id="custom_setters"> |
| 1549 | Custom Setters |
| 1550 | </h3> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1551 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1552 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1556 | A static binding adapter method with the {@link android.databinding.BindingAdapter} |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1557 | annotation allows the developer to customize how a setter for an attribute is |
| 1558 | called. |
| 1559 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1560 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1561 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1567 | @BindingAdapter("android:paddingLeft") |
| 1568 | public static void setPaddingLeft(View view, int padding) { |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1569 | view.setPadding(padding, |
| 1570 | view.getPaddingTop(), |
| 1571 | view.getPaddingRight(), |
| 1572 | view.getPaddingBottom()); |
| 1573 | } |
| 1574 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1575 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1579 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1580 | <p> |
| 1581 | Developer-created binding adapters will override the data binding default |
| 1582 | adapters when there is a conflict. |
| 1583 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1584 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1585 | <p> |
| 1586 | You can also have adapters that receive multiple parameters. |
| 1587 | </p> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1588 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1589 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1590 | @BindingAdapter({"bind:imageUrl", "bind:error"}) |
| 1591 | public static void loadImage(ImageView view, String url, Drawable error) { |
| 1592 | Picasso.with(view.getContext()).load(url).error(error).into(view); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1593 | } |
| 1594 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1595 | <pre> |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1596 | <ImageView app:imageUrl="@{venue.imageUrl}" |
| 1597 | app:error="@{@drawable/venueError}"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1598 | </pre> |
| 1599 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1600 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1605 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1606 | <ul> |
| 1607 | <li>Custom namespaces are ignored during matching. |
| 1608 | </li> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1609 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1610 | <li>You can also write adapters for android namespace. |
| 1611 | </li> |
| 1612 | </ul> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1613 | |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1614 | <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 | @BindingAdapter("android:paddingLeft") |
| 1621 | public 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 | @BindingAdapter("android:onLayoutChange") |
| 1636 | public 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 | @TargetApi(VERSION_CODES.HONEYCOMB_MR1) |
| 1658 | public interface OnViewDetachedFromWindow { |
| 1659 | void onViewDetachedFromWindow(View v); |
| 1660 | } |
| 1661 | |
| 1662 | @TargetApi(VERSION_CODES.HONEYCOMB_MR1) |
| 1663 | public 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 | @BindingAdapter("android:onViewAttachedToWindow") |
| 1673 | public static void setListener(View view, OnViewAttachedToWindow attached) { |
| 1674 | setListener(view, null, attached); |
| 1675 | } |
| 1676 | |
| 1677 | @BindingAdapter("android:onViewDetachedFromWindow") |
| 1678 | public static void setListener(View view, OnViewDetachedFromWindow detached) { |
| 1679 | setListener(view, detached, null); |
| 1680 | } |
| 1681 | |
| 1682 | @BindingAdapter({"android:onViewDetachedFromWindow", "android:onViewAttachedToWindow"}) |
| 1683 | public 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 | @Override |
| 1692 | public void onViewAttachedToWindow(View v) { |
| 1693 | if (attach != null) { |
| 1694 | attach.onViewAttachedToWindow(v); |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | @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>@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 Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1731 | <h2 id="converters"> |
| 1732 | Converters |
| 1733 | </h2> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1734 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1735 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1751 | <TextView |
| Mark Lu | 011c07b | 2016-07-28 11:15:11 -0700 | [diff] [blame] | 1752 | android:text='@{userMap["lastName"]}' |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1753 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1754 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1755 | </pre> |
| 1756 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1757 | <p> |
| 1758 | The <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1763 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1764 | <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 Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1772 | <View |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1773 | android:background="@{isError ? @color/red : @color/white}" |
| 1774 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1775 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1776 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1777 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1784 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1785 | <pre> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1786 | @BindingConversion |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1787 | public static ColorDrawable convertColorToDrawable(int color) { |
| 1788 | return new ColorDrawable(color); |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1789 | } |
| 1790 | </pre> |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1791 | <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 Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1795 | |
| Dirk Dougherty | ea1f6fb | 2015-05-26 20:31:12 -0700 | [diff] [blame] | 1796 | <pre> |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1797 | <View |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1798 | android:background="@{isError ? @drawable/error : @color/white}" |
| 1799 | android:layout_width="wrap_content" |
| George Mount | 4ba1820 | 2015-07-27 12:39:28 -0700 | [diff] [blame] | 1800 | android:layout_height="wrap_content"/> |
| Dirk Dougherty | 493ff3c | 2015-05-26 16:52:23 -0700 | [diff] [blame] | 1801 | </pre> |
| Daniel Yu | daa41bb | 2015-12-08 10:58:07 -0800 | [diff] [blame] | 1802 | |
| 1803 | <h3 id="studio_support">Android Studio Support for Data Binding</h3> |
| 1804 | |
| 1805 | <p> |
| Cheryl Potter | 8dd7eea | 2016-04-04 17:10:06 -0700 | [diff] [blame] | 1806 | Android Studio supports many of the code editing features for data binding code. |
| 1807 | For 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" |
| 1813 | href="https://www.jetbrains.com/help/idea/2016.1/navigation-in-source-code.html?origin=old_help"> |
| 1814 | navigation</a> (such as navigate to a declaration) and |
| 1815 | <a class="external-link" |
| 1816 | href="https://www.jetbrains.com/help/idea/2016.1/viewing-inline-documentation.html?origin=old_help"> |
| 1817 | quick 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" |
| 1822 | class="external-link">generic type</a>, such as the {@link |
| 1823 | android.databinding.Observable} class, might display |
| 1824 | errors when there are no errors.</p> |
| 1825 | |
| Daniel Yu | daa41bb | 2015-12-08 10:58:07 -0800 | [diff] [blame] | 1826 | <p> |
| Cheryl Potter | 8dd7eea | 2016-04-04 17:10:06 -0700 | [diff] [blame] | 1827 | The Preview pane displays default values for data binding expressions if |
| 1828 | provided. In the following |
| 1829 | example excerpt of an element from a layout XML file, the Preview pane displays |
| 1830 | the {@code PLACEHOLDER} default text value in the <code>TextView</code>. |
| Daniel Yu | daa41bb | 2015-12-08 10:58:07 -0800 | [diff] [blame] | 1831 | </p> |
| 1832 | |
| 1833 | <pre> |
| 1834 | <TextView android:layout_width="wrap_content" |
| 1835 | android:layout_height="wrap_content" |
| 1836 | android:text="@{user.firstName, default=PLACEHOLDER}"/> |
| 1837 | </pre> |
| 1838 | |
| 1839 | <p> |
| Cheryl Potter | 8dd7eea | 2016-04-04 17:10:06 -0700 | [diff] [blame] | 1840 | If you need to display a default value during the design phase of your project, |
| 1841 | you can also use tools attributes instead of default expression values, as |
| 1842 | described in |
| 1843 | <a class="external-link" |
| 1844 | href="http://tools.android.com/tips/layout-designtime-attributes"> |
| Daniel Yu | daa41bb | 2015-12-08 10:58:07 -0800 | [diff] [blame] | 1845 | Designtime Layout Attributes</a>. |
| Cheryl Potter | 8dd7eea | 2016-04-04 17:10:06 -0700 | [diff] [blame] | 1846 | </p> |
| 1847 | |