1 <html devsite> 2 <head> 3 <title>Build a Responsive UI with ConstraintLayout</title> 4 <meta name="book_path" value="/training/_book.yaml" /> 5 <meta name="top_category" value="develop" /> 6 <meta name="subcategory" value="training" /> 7 </head> 8 <body> 9 10 <div id="tb-wrapper"> 11 <div id="tb"> 12 <h2>In this document</h2> 13 <ol> 14 <li><a href="#constraints-overview">Constraints overview</a></li> 15 <li><a href="#add-constraintlayout-to-your-project">Add ConstraintLayout to your project</a></li> 16 <li><a href="#add-a-constraint">Add a constraint</a></li> 17 <li><a href="#use-autoconnect-and-infer-constraints">Use Autoconnect and Infer Constraints</a></li> 18 <li><a href="#adjust-the-view-size">Adjust the view size</a></li> 19 <li><a href="#adjust-the-constraint-bias">Adjust the constraint bias</a></li> 20 <li><a href="#adjust-the-view-margins">Adjust the view margins</a></li> 21 </ol> 22 </div> 23 </div> 24 25 26 <p><code>ConstraintLayout</code> allows you to create large and complex layouts with a flat view 27 hierarchy (no nested view groups). It's similar to <code>RelativeLayout</code> in that all views are 28 layed out according to relationships between sibling views and the parent layout, but it's more 29 flexible than <code>RelativeLayout</code> and easier to use with Android Studio's Layout Editor. 30 </p> 31 32 <p>Everything you can do with <code>ConstraintLayout</code> is available directly from the Layout Editor's visual 33 tools, because the layout API and the Layout Editor were specially built for each other. So you can 34 build your layout with <code>ConstraintLayout</code> entirely by drag-and-dropping instead of editing the XML. 35 </p> 36 37 <img src="/training/constraint-layout/images/layout-editor_2-2_2x.png" alt="" 38 width="640"/> 39 <p class="img-caption"><b>Figure 1.</b> A <code>ConstraintLayout</code> in the Layout Editor</p> 40 41 42 <p> 43 <code>ConstraintLayout</code> is available in an API library that's compatible with Android 44 2.3 (API level 9) and higher, and the new layout editor is available in Android 45 Studio 2.2 and higher. 46 </p> 47 48 <p> 49 This page provides a guide to building a layout with <code>ConstraintLayout</code> in Android 50 Studio. If you'd like more information about the Layout Editor itself, see the 51 Android Studio guide to <a href="/studio/write/layout-editor.html">Build a UI with 52 Layout Editor</a>. 53 </p> 54 55 56 <h2 id="constraints-overview">Constraints overview</h2> 57 <p> 58 To define a view's position in <code>ConstraintLayout</code>, you must add two 59 or more <em>constraints</em> for the view. Each constraint represents a connection or 60 alignment to another view, the parent layout, or an invisible guideline. Each 61 constraint defines the view's position along either the 62 vertical or horizontal axis; so each view must have a minimum of one constraint for each 63 axis, but often more are necessary. 64 </p> 65 66 <p> 67 When you drop a view into the Layout Editor, it stays where you leave it even if 68 it has no constraints. However, this is only to make editing easier; if a view has 69 no constraints when you run your layout on a device, it is drawn at 70 position [0,0] (the top-left corner).</p> 71 72 <p>In figure 2, the layout looks good in the 73 editor, but there's no vertical constraint on <code>TextView B</code>. When this 74 layout draws on a device, <code>TextView B</code> horizontally aligns with the left and 75 right edges of the <code>ImageView</code>, but appears at the top of the screen because 76 it has no vertical constraint. 77 </p> 78 79 <div class="cols"> 80 <div class="col-1of2"> 81 <img src="/training/constraint-layout/images/constraint-fail_2x.png" width="100%" alt="" /> 82 <p class="img-caption"><strong>Figure 2.</strong> <code>TextView B</code> is missing a 83 vertical constraint</p> 84 </div> 85 <div class="col-1of2"> 86 <img src="/training/constraint-layout/images/constraint-fail-fixed_2x.png" width="100%" alt="" /> 87 <p class="img-caption"><strong>Figure 3.</strong> <code>TextView B</code> is now vertically 88 constrained to the <code>ImageView</code></p> 89 </div> 90 </div> 91 92 <p> 93 Although a missing constraint won't cause a compilation error, the Layout Editor 94 indicates missing constraints as an error in the toolbar. To view the errors and 95 other warnings, click <strong>Show Warnings and Errors</strong> 96 <img src="/studio/images/buttons/layout-editor-errors.png" class="inline-icon" alt="" />. 97 To help you avoid missing constraints, the Layout Editor can automatically add 98 constraints for you with the <a 99 href="#use-autoconnect-and-infer-constraints">Autoconnect and infer 100 constraints</a> features. 101 </p> 102 103 104 <h2 id="add-constraintlayout-to-your-project">Add ConstraintLayout to your project</h2> 105 <p> 106 To use <code>ConstraintLayout</code> in your project, proceed as follows: 107 </p> 108 109 <ol> 110 <li>Ensure you have the latest Constraint Layout library: 111 <ol> 112 <li>Click <strong>Tools > Android > SDK Manager</strong>. 113 <li>Click the <strong>SDK Tools</strong> tab. 114 <li>Expand <strong>Support Repository</strong> and then check 115 <b>ConstraintLayout for Android</b> and <b>Solver for ConstraintLayout</b>. 116 Check <b>Show Package Details</b> and take note of the version you're downloading 117 (you'll need this below).</p> 118 <li>Click <strong>OK</strong>. 119 <li>Add the ConstraintLayout library as a dependency in your module-level 120 <code>build.gradle</code> file: 121 <pre> 122 dependencies { 123 compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8' 124 } 125 </pre> 126 <p>The library version you download may be higher, so be sure the value you specify 127 here matches the version from step 3.</p> 128 </li> 129 <li>In the toolbar or sync notification, click <strong>Sync Project with Gradle 130 Files</strong>.</li> 131 </ol> 132 </li> 133 </ol> 134 135 <p>Now you're ready to build your layout with <code>ConstraintLayout</code>.</p> 136 137 <h3 id="convert">Convert a layout</h3> 138 139 <div class="figure" style="width:415px"> 140 <img src="/training/constraint-layout/images/layout-editor-convert-to-constraint_2x.png" 141 width="415" alt="" /> 142 <p class="img-caption"> 143 <b>Figure 4.</b> The menu to convert a layout to <code>ConstraintLayout</code></p> 144 </div> 145 146 <p>To convert an existing layout to a constraint layout, follow these steps:</p> 147 <ol> 148 <li>Open your layout in Android Studio and click the <strong>Design</strong> tab 149 at the bottom of the editor window. 150 <li>In the <strong>Component Tree</strong> window, right-click the layout and 151 click <strong>Convert <em>layout</em> to ConstraintLayout</strong>.</li> 152 </ol> 153 154 <h3 id="createNew">Create a new layout</h3> 155 156 <p>To start a new constraint layout file, follow these steps:</p> 157 <ol> 158 <li>Click anywhere in the <strong>Project</strong> window and then select 159 <strong>File > New > XML > Layout XML</strong>. 160 <li>Enter a name for the layout file and enter 161 "android.support.constraint.ConstraintLayout" for the <b>Root Tag</b>. 162 <li>Click <strong>Finish</strong>.</li> 163 </ol> 164 165 166 <h2 id="add-a-constraint">Add a constraint</h2> 167 168 <p>Start by dragging a view from the <b>Palette</b> window into the editor. 169 When you add a view in a <code>ConstraintLayout</code>, it displays a bounding box with square 170 resizing handles on each corner and circular constraint handles on each side. 171 </p> 172 173 174 <div class="figure" style="width:460px"> 175 <div class="video-wrapper"> 176 <video controls poster="/training/constraint-layout/images/thumbnail-studio-constraint-first.png" 177 onclick="this.play()" width="460"> 178 <source src="https://storage.googleapis.com/androiddevelopers/videos/studio/studio-constraint-first.mp4" type="video/mp4"> 179 <img src="/training/constraint-layout/images/thumbnail-studio-constraint-first" alt="" /> 180 </video> 181 </div> 182 <p class="img-caption"> 183 <strong>Video 1. </strong>The left side of a view is constrained to the left side of the parent 184 </p> 185 </div> 186 187 <p> 188 Click the view to select it. Then click-and-hold one of the 189 constraint handles and drag the line to an available anchor point (the edge of 190 another view, the edge of the layout, or a guideline). When you release, the 191 constraint is made, with <a href="#adjust-the-view-margins">a default margin</a> 192 separating the two views. 193 </p> 194 195 <p>When creating constraints, remember the following rules:</p> 196 197 <ul> 198 <li>Every view must have at least two constraints: one horizontal and one 199 vertical. 200 <li>You can create constraints only between a constraint handle and an anchor 201 point that share the same plane. So a vertical plane (the left and right sides) 202 of a view can be constrained only to another vertical plane; and baselines can 203 constrain only to other baselines. 204 <li>Each constraint handle can be used for just one constraint, but you can 205 create multiple constraints (from different views) to the same anchor 206 point.</li> 207 </ul> 208 209 210 211 <div class="figure" style="width:460px"> 212 <div class="video-wrapper"> 213 <video controls poster="/training/constraint-layout/images/thumbnail-studio-constraint-second.png" 214 onclick="this.play()" width="460"> 215 <source src="https://storage.googleapis.com/androiddevelopers/videos/studio/studio-constraint-second.mp4" type="video/mp4"> 216 <img src="/training/constraint-layout/images/thumbnail-studio-constraint-second.png" alt="" /> 217 </video> 218 </div> 219 <p class="img-caption"> 220 <strong>Video 2. </strong>Adding a constraint that opposes an existing one 221 </p> 222 </div> 223 224 225 226 <p> 227 To remove a constraint, select the view and then click the constraint handle. 228 </p> 229 230 <p>If you add opposing constraints on a view, the constraint lines become squiggly 231 like a spring to indicate the opposing forces, as shown in video 2. The effect 232 is most visible when the view size is set to "fixed" or "wrap content," in which 233 case the view is centered between the constraints. If you instead 234 want the view to stretch its size to meet the constraints, <a 235 href="#adjust-the-view-size">switch the size to "any size"</a>; or if you want 236 to keep the current size but move the view so that it is not centered, <a 237 href="#adjust-the-constraint-bias">adjust the constraint bias</a>. 238 </p> 239 240 241 242 <p> 243 There are many ways to constrain a view, but the following constraint types 244 provide the basic building blocks. 245 </p> 246 247 248 249 250 <h3>Parent constraint</h3> 251 <div class="cols"> 252 <div class="col-2of3"> 253 <p> 254 Connect the side of a view to the corresponding edge of the layout. 255 <p> 256 In figure 5, the left side of a view is connected to the left edge of the 257 parent layout. 258 <p> 259 </div> 260 <div class="col-1of3"> 261 <img src="/training/constraint-layout/images/parent-constraint_2x.png" width="100%" alt=""> 262 <p class="img-caption"><strong>Figure 5. </strong>A horizontal constraint to the parent</p> 263 </div> 264 </div> 265 266 267 <h3>Position constraint</h3> 268 <div class="cols"> 269 <div class="col-2of3"> 270 <p>Define the order of appearance for two views, either vertically or horizontally.</p> 271 <p>In figure 6, a <code>Button</code> is constrained below an <code>ImageView</code> with a 24dp 272 margin.</p> 273 </div> 274 <div class="col-1of3"> 275 <img src="/training/constraint-layout/images/position-constraint_2x.png" width="100%" alt=""> 276 <p class="img-caption"><strong>Figure 6.</strong> A vertical position constraint</p> 277 </div> 278 </div> 279 280 281 282 <h3>Alignment constraint</h3> 283 <div class="cols"> 284 <div class="col-1of3"> 285 <p>Align the edge of a view to the same edge of another view.<p> 286 <p>In figure 7, the left side of a <code>Button</code> is aligned to the left side of an 287 <code>ImageView</code>.</p> 288 <p>You can offset the alignment by dragging the view 289 inward from the constraint. For example, figure 8 shows the same 290 <code>Button</code> with a 24dp offset alignment. 291 The offset is defined by the constrained view's margin.</p> 292 </div> 293 <div class="col-1of3"> 294 <img src="/training/constraint-layout/images/alignment-constraint_2x.png" width="100%" alt=""> 295 <p class="img-caption"><strong>Figure 7.</strong> A horizontal alignment constraint</p> 296 </div> 297 <div class="col-1of3"> 298 <img src="/training/constraint-layout/images/alignment-constraint-offset_2x.png" width="100%" 299 alt=""> 300 <p class="img-caption"><strong>Figure 8.</strong> An offset horizontal alignment constraint</p> 301 </div> 302 </div> 303 304 305 <h3>Baseline alignment constraint</h3> 306 <div class="cols"> 307 <div class="col-2of3"> 308 <p>Align the text baseline of a view to the text baseline of another view.</p> 309 <p>In figure 9, the first line of a <code>TextView</code> is aligned with the text in a 310 <code>Button</code>.</p> 311 <p>To create a baseline constraint, hover your mouse over the baseline handle for 312 two seconds until the handle blinks white. Then click and drag the line to 313 another baseline.</p> 314 </div> 315 <div class="col-1of3"> 316 <img src="/training/constraint-layout/images/baseline-constraint_2x.png" width="100%" alt=""> 317 <p class="img-caption"><strong>Figure 9.</strong> A baseline alignment constraint</p> 318 </div> 319 </div> 320 321 322 323 324 325 <h3 id="constrain-to-a-guideline">Constrain to a guideline</h3> 326 <div class="cols"> 327 <div class="col-1of2"> 328 329 <p> 330 You can add a vertical or horizontal guideline to which you can attach 331 constraints. You can position the guideline within the layout based on either dp 332 units or percent, relative to the layout's edge. 333 </p> 334 335 <p> 336 To create a guideline, click <strong>Guidelines</strong> 337 <img src="/studio/images/buttons/layout-editor-guidelines.png" class="inline-icon" alt="" /> 338 in the toolbar, and then click either <strong>Add Vertical Guideline</strong> 339 or <strong>Add Horizontal Guideline</strong>. 340 </p> 341 342 <p> 343 Click the circle at the edge of the guideline to toggle the measurements used to 344 position the guideline (either percent or dp units from the layout's edge). 345 </p> 346 347 <p> 348 Guidelines are not visible to your users. 349 </p> 350 </div> 351 352 <div class="col-1of2"> 353 <div class="video-wrapper"> 354 <video controls poster="/training/constraint-layout/images/thumbnail-add-layout-guideline_2-2.png" 355 onclick="this.play()" width="100%"> 356 <source src="https://storage.googleapis.com/androiddevelopers/videos/studio/add-layout-guideline_2-2.mp4" type="video/mp4"> 357 <img src="/training/constraint-layout/images/thumbnail-add-layout-guideline_2-2.png" alt="" /> 358 </video> 359 </div> 360 <p class="img-caption"><strong>Video 3.</strong> Adding a constraint to a guideline</p> 361 </div> 362 </div> 363 364 365 <h2 id="use-autoconnect-and-infer-constraints">Use Autoconnect and Infer Constraints</h2> 366 367 <div class="figure" style="width:460px"> 368 <div class="video-wrapper"> 369 <video controls poster="" 370 onclick="this.play()" width="460"> 371 <source src="https://storage.googleapis.com/androiddevelopers/videos/studio/constraint-autoconnect_2-2.mp4" type="video/mp4"> 372 </video> 373 </div> 374 <p class="img-caption"><b>Video 4.</b> Adding a view with Autoconnect enabled</p> 375 </div> 376 377 <p> 378 Autoconnect is a persistent mode that automatically creates two or more 379 constraints for each view you add to the layout. Autoconnect is disabled by 380 default. You can enable it by clicking <strong>Turn on Autoconnect</strong> 381 <img src="/studio/images/buttons/layout-editor-autoconnect-on.png" class="inline-icon" alt="" /> 382 in the Layout Editor toolbar. 383 </p> 384 385 <p>While enabled, Autoconnect creates constraints for each view as you add them; it does not create 386 constraints for existing views in the layout. If you drag a view once the constraints are made, the 387 constraints do not change (though the margins do), so you must delete the constraints if you want to 388 significantly reposition the view.</p> 389 390 <p>Alternatively, you can click <strong>Infer Constraints</strong> 391 <img src="/studio/images/buttons/layout-editor-infer.png" class="inline-icon" alt="" /> 392 to create constraints for all views in the layout. 393 </p> 394 395 <p>Infer Constraints is a one-time action that scans the entire layout to determine the most 396 effective set of constraints for all views, so it may create constraints between elements that are 397 far from each other. Autoconnect, however, creates constraints only for the view you are adding, and 398 it creates constraints to only the nearest elements. In either case, you can always modify a 399 constraint by clicking the constraint handle to delete it, and then create a new constraint.</p> 400 401 402 <h2 id="adjust-the-view-size">Adjust the view size</h2> 403 404 <p> 405 You can use the handles on each corner of the view to resize it, but doing so 406 hard codes the width and height values, which you should avoid for most views 407 because hard-coded view sizes cannot adapt to different content and screen 408 sizes. To select from one of the dynamic sizing modes or to define more specific 409 dimensions, click a view and open the <strong>Properties</strong> 410 <img src="/studio/images/buttons/window-properties.png" class="inline-icon" alt="" /> 411 window on the right side of the editor. At the top of the window is the view 412 inspector, as shown in figure 10. 413 </p> 414 <div class="figure" style="width:287px" > 415 <img src="/training/constraint-layout/images/layout-editor-properties-callouts_2-2_2x.png" alt="" 416 width="287" /> 417 <p class="img-caption"><strong>Figure 10.</strong> The <b>Properties</b> window includes controls for 418 <strong>(1)</strong> view size, <strong>(2)</strong> margins, and 419 <strong>(3)</strong> constraint bias.</p> 420 </div> 421 422 <p> 423 The grey square represents the selected view. The symbols inside the square 424 represent the height and width settings as follows: 425 </p> 426 427 <ul> 428 <li> 429 <img src="/studio/images/buttons/layout-width-wrap.png" class="inline-icon" alt="" /> 430 <strong>Wrap Content</strong>: The view expands exactly as needed to fit its 431 contents. 432 <li> 433 <img src="/studio/images/buttons/layout-width-match.png" class="inline-icon" alt="" /> 434 <strong>Any Size</strong>: The view expands exactly as needed to match the 435 constraints. The actual value is 0dp because the view has no desired dimensions, but 436 it resizes as needed to meet the constraints. However, if the given dimension 437 has only one constraint, then the view expands to fit its contents. Another way 438 to think of it is "match constraints" (instead of <code>match_parent</code>) because it 439 expands the view as much as possible after accounting for the limits of each 440 constraint and its margins. 441 <li> 442 <img src="/studio/images/buttons/layout-width-fixed.png" class="inline-icon" alt="" /> 443 <strong>Fixed</strong>: You specify the dimension in the text box below or by 444 resizing the view in the editor.</li> 445 </ul> 446 447 <p>To toggle between these settings, click the symbols.</p> 448 449 <p class="note"><strong>Note</strong>: You should not use <code>match_parent</code> for any view 450 in a <code>ConstraintLayout</code>. Instead use "Any Size" (<code>0dp</code>). 451 </p> 452 453 454 <h2 id="adjust-the-constraint-bias">Adjust the constraint bias</h2> 455 456 <p>When you add a constraint to both sides of a view (and the view size for the same dimension is 457 either "fixed" or "wrap content"), the view becomes centered between the two anchor points by 458 default. When a view is centered, the bias is 50%. You can adjust the bias by dragging the bias 459 slider in the <b>Properties</b> window or by dragging the view, as shown in video 5.</p> 460 461 <div class="video-wrapper" style="max-width:740px"> 462 <video controls poster="/training/constraint-layout/images/thumbnail-adjust-constraint-bias.png" 463 onclick="this.play();$(this.parentElement).addClass('playing');"> 464 <source src="https://storage.googleapis.com/androiddevelopers/videos/studio/adjust-constraint-bias.mp4" type="video/mp4"> 465 <img src="/training/constraint-layout/images/thumbnail-adjust-constraint-bias.png" alt="" /> 466 </video> 467 </div> 468 <p class="img-caption"><b>Video 5.</b> Adjusting the constraint bias</p> 469 470 <p>If you instead want the view to stretch its size to meet the constraints, <a href="#adjust-the- 471 view-size">switch the size to "any size"</a>.</p> 472 473 474 <h2 id="adjust-the-view-margins">Adjust the view margins</h2> 475 476 <p> To ensure that all your views are evenly spaced, click <strong>Margin</strong> <img 477 src="/studio/images/buttons/layout-editor-margin.png" class="inline-icon" alt="" /> in the toolbar 478 to select the default margin for each view that you add to the layout. The button changes to show 479 your current margin selection. Any change you make to the default margin applies only to the views 480 you add from then on. </p> 481 482 483 <img src="/training/constraint-layout/images/layout-editor-margin-callout_2-2_2x.png" 484 alt="" width="232"/> 485 <p class="img-caption"><strong>Figure 11.</strong> The toolbar's <b>Margin</b> button. 486 Click to adjust the default margin. 487 </p> 488 489 <p> You can control the margin for each view in the <strong>Properties</strong> window by clicking 490 the number on the line that represents each constraint (in figure 10, the margins are each set to 491 16dp). </p> 492 493 <p> All margins offered by the tool are factors of 8dp to help your views align to Material Design's 494 <a href="https://material.google.com/layout/metrics-keylines.html">8dp square grid 495 recommendations</a>. </p> 496 497 </body> 498 </html> 499