• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
27hierarchy (no nested view groups). It's similar to <code>RelativeLayout</code> in that all views are
28layed out according to relationships between sibling views and the parent layout, but it's more
29flexible 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
33tools, because the layout API and the Layout Editor were specially built for each other. So you can
34build 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
442.3 (API level 9) and higher, and the new layout editor is available in Android
45Studio 2.2 and higher.
46</p>
47
48<p>
49This page provides a guide to building a layout with <code>ConstraintLayout</code> in Android
50Studio. If you'd like more information about the Layout Editor itself, see the
51Android Studio guide to <a href="/studio/write/layout-editor.html">Build a UI with
52Layout Editor</a>.
53</p>
54
55
56<h2 id="constraints-overview">Constraints overview</h2>
57<p>
58To define a view's position in <code>ConstraintLayout</code>, you must add two
59or more <em>constraints</em> for the view. Each constraint represents a connection or
60alignment to another view, the parent layout, or an invisible guideline. Each
61constraint defines the view's position along either the
62vertical or horizontal axis; so each view must have a minimum of one constraint for each
63axis, but often more are necessary.
64</p>
65
66<p>
67When you drop a view into the Layout Editor, it stays where you leave it even if
68it has no constraints. However, this is only to make editing easier; if a view has
69no constraints when you run your layout on a device, it is drawn at
70position [0,0] (the top-left corner).</p>
71
72<p>In figure 2, the layout looks good in the
73editor, but there's no vertical constraint on <code>TextView B</code>. When this
74layout draws on a device, <code>TextView B</code> horizontally aligns with the left and
75right edges of the <code>ImageView</code>, but appears at the top of the screen because
76it 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
83vertical 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
88constrained to the <code>ImageView</code></p>
89</div>
90</div>
91
92<p>
93Although a missing constraint won't cause a compilation error, the Layout Editor
94indicates missing constraints as an error in the toolbar. To view the errors and
95other warnings, click <strong>Show Warnings and Errors</strong>
96<img src="/studio/images/buttons/layout-editor-errors.png" class="inline-icon" alt="" />.
97To help you avoid missing constraints, the Layout Editor can automatically add
98constraints for you with the <a
99href="#use-autoconnect-and-infer-constraints">Autoconnect and infer
100constraints</a> features.
101</p>
102
103
104<h2 id="add-constraintlayout-to-your-project">Add ConstraintLayout to your project</h2>
105<p>
106To 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>.
116Check <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>
122dependencies {
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
130Files</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
149at the bottom of the editor window.
150<li>In the <strong>Component Tree</strong> window, right-click the layout and
151click <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.
169When you add a view in a <code>ConstraintLayout</code>, it displays a bounding box with square
170resizing 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>
188Click the view to select it. Then click-and-hold one of the
189constraint handles and drag the line to an available anchor point (the edge of
190another view, the edge of the layout, or a guideline). When you release, the
191constraint is made, with <a href="#adjust-the-view-margins">a default margin</a>
192separating 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
199vertical.
200<li>You can create constraints only between a constraint handle and an anchor
201point that share the same plane. So a vertical plane (the left and right sides)
202of a view can be constrained only to another vertical plane; and baselines can
203constrain only to other baselines.
204<li>Each constraint handle can be used for just one constraint, but you can
205create multiple constraints (from different views) to the same anchor
206point.</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>
227To 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
231like a spring to indicate the opposing forces, as shown in video 2. The effect
232is most visible when the view size is set to "fixed" or "wrap content," in which
233case the view is centered between the constraints. If you instead
234want the view to stretch its size to meet the constraints, <a
235href="#adjust-the-view-size">switch the size to "any size"</a>; or if you want
236to keep the current size but move the view so that it is not centered, <a
237href="#adjust-the-constraint-bias">adjust the constraint bias</a>.
238</p>
239
240
241
242<p>
243There are many ways to constrain a view, but the following constraint types
244provide 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
272margin.</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
289inward from the constraint. For example, figure 8 shows the same
290<code>Button</code> with a 24dp offset alignment.
291The 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
312two seconds until the handle blinks white. Then click and drag the line to
313another 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>
330You can add a vertical or horizontal guideline to which you can attach
331constraints. You can position the guideline within the layout based on either dp
332units or percent, relative to the layout's edge.
333</p>
334
335<p>
336To create a guideline, click <strong>Guidelines</strong>
337<img src="/studio/images/buttons/layout-editor-guidelines.png" class="inline-icon" alt="" />
338in the toolbar, and then click either <strong>Add Vertical Guideline</strong>
339or <strong>Add Horizontal Guideline</strong>.
340</p>
341
342<p>
343Click the circle at the edge of the guideline to toggle the measurements used to
344position the guideline (either percent or dp units from the layout's edge).
345</p>
346
347<p>
348Guidelines 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>
378Autoconnect is a persistent mode that automatically creates two or more
379constraints for each view you add to the layout. Autoconnect is disabled by
380default. 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="" />
382in 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
386constraints for existing views in the layout. If you drag a view once the constraints are made, the
387constraints do not change (though the margins do), so you must delete the constraints if you want to
388significantly 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="" />
392to 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
396effective set of constraints for all views, so it may create constraints between elements that are
397far from each other. Autoconnect, however, creates constraints only for the view you are adding, and
398it creates constraints to only the nearest elements. In either case, you can always modify a
399constraint 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>
405You can use the handles on each corner of the view to resize it, but doing so
406hard codes the width and height values, which you should avoid for most views
407because hard-coded view sizes cannot adapt to different content and screen
408sizes. To select from one of the dynamic sizing modes or to define more specific
409dimensions, click a view and open the <strong>Properties</strong>
410<img src="/studio/images/buttons/window-properties.png" class="inline-icon" alt="" />
411window on the right side of the editor. At the top of the window is the view
412inspector, 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>
423The grey square represents the selected view. The symbols inside the square
424represent 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
431contents.
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
435constraints. The actual value is 0dp because the view has no desired dimensions, but
436it resizes as needed to meet the constraints. However, if the given dimension
437has only one constraint, then the view expands to fit its contents. Another way
438to think of it is "match constraints" (instead of <code>match_parent</code>) because it
439expands the view as much as possible after accounting for the limits of each
440constraint 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
444resizing 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
450in 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
457either "fixed" or "wrap content"), the view becomes centered between the two anchor points by
458default. When a view is centered, the bias is 50%. You can adjust the bias by dragging the bias
459slider 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-
471view-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
477src="/studio/images/buttons/layout-editor-margin.png" class="inline-icon" alt="" /> in the toolbar
478to select the default margin for each view that you add to the layout. The button changes to show
479your current margin selection. Any change you make to the default margin applies only to the views
480you 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.
486Click 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
490the number on the line that represents each constraint (in figure 10, the margins are each set to
49116dp). </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
495recommendations</a>. </p>
496
497</body>
498</html>
499