Site icon C1CTech

Android Getting Started With Constraint Layout

<p class&equals;"p1">In this tutorial&comma;<span style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;"> <&sol;span><span style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;">you’ll learn the basics of creating Android views by using ConstraintLayout to build the UI of your application&period;<&sol;span><&sol;p>&NewLine;<p class&equals;"p1">A great Android app not only needs to have a beautiful UI but also optimized performance&period; Android provides various types of layouts that use different methods to contain child views and determine their positions&period; Layouts all descend from the <span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;view&sol;ViewGroup"><span class&equals;"s1"><b>ViewGroup<&sol;b><&sol;span><&sol;a><&sol;span> class&period;<&sol;p>&NewLine;<p class&equals;"p1">Some of the most common layouts to use when building your Android UI are <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;widget&sol;FrameLayout">FrameLayout<&sol;a><&sol;strong>&comma;<&sol;span> <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;widget&sol;LinearLayout">LinearLayout<&sol;a><&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;guide&sol;topics&sol;ui&sol;layout&sol;relative">RelativeLayout<&sol;a><&sol;strong>&period;<&sol;span> They are easy to use&comma; but they each have certain limitations and performance issues when the view hierarchy becomes complex&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;008000&semi;">FrameLayout<&sol;span><&sol;strong> can only position child views by applying gravity relative to their parent&period;<&sol;li>&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;008000&semi;"><strong>LinearLayout<&sol;strong><&sol;span> doesn’t allow views to overlap one another&period; But while working with complex UI we make use of multiple nested LinearLayouts which decreases performance&period;<&sol;li>&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;constraintlayout&sol;widget&sol;ConstraintLayout">ConstraintLayout<&sol;a><&sol;span><&sol;strong> is similar to <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">RelativeLayout<&sol;span><&sol;strong><&sol;span> in that all views are laid out according to relationships between sibling views and the parent layout&comma; but it&&num;8217&semi;s more flexible than <span class&equals;"s2">RelativeLayout<&sol;span> and easier to use with Android Studio&&num;8217&semi;s Layout Editor&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">What is ConstraintLayout&quest;<&sol;span><&sol;strong><&sol;h3>&NewLine;<p class&equals;"p1">A <strong><span style&equals;"color&colon; &num;008000&semi;">ConstraintLayout<&sol;span><&sol;strong> is a ViewGroup that allows you to position and size widgets in a flexible way&period;<&sol;p>&NewLine;<p class&equals;"p1">It allows you to create large and complex layouts with a flat view hierarchy means there are no nested view groups&period;<&sol;p>&NewLine;<p class&equals;"p1">All the power of ConstraintLayout is available directly from the Layout Editor&&num;8217&semi;s visual tools because the layout API and the Layout Editor were specially built for each other&period; So you can build your layout with ConstraintLayout entirely by drag-and-dropping instead of editing the XML&period;<&sol;p>&NewLine;<h3 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><b>Add ConstraintLayout to your project<&sol;b><&sol;span><&sol;h3>&NewLine;<p class&equals;"p3">To use ConstraintLayout in your project&comma; add the library as a dependency in the app level <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span> file&comma; as shown in the example below&period;<&sol;p>&NewLine;<p class&equals;"p3">Note that the latest version might be different than what is shown in the example&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>build&period;gradle<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>dependencies &lbrace;&NewLine; &NewLine; implementation 'androidx&period;constraintlayout&colon;constraintlayout&colon;1&period;1&period;3'&NewLine; &NewLine;&rcub;<&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Constraints<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Constraints<&sol;span><&sol;strong> are the basic building blocks of ConstraintLayout&period; A constraint represents a connection or alignment to another view&comma; the parent layout&comma; or an invisible guideline&period; Each constraint defines the view&&num;8217&semi;s position along either the vertical or horizontal axis&period;<&sol;p>&NewLine;<p class&equals;"p1">To define a view&&num;8217&semi;s position in <span class&equals;"s1">ConstraintLayout<&sol;span>&comma; you must add at least one horizontal and one vertical constraint for the view&period;<&sol;p>&NewLine;<p class&equals;"p1">If a view has no constraints when you run your layout on a device&comma; it is drawn at position &lbrack;0&comma;0&rsqb; &lpar;the top-left corner&rpar;&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Rules for constraints<&sol;strong><&sol;span><&sol;h4>&NewLine;<ul>&NewLine;<li>Every view must have at least two constraints&colon; one horizontal and one vertical&period;<&sol;li>&NewLine;<li>You can create constraints only between a <strong><span style&equals;"color&colon; &num;008000&semi;">constraint handle<&sol;span><&sol;strong> &lpar;<strong>circle on each side of an element<&sol;strong>&rpar; and an <strong><span style&equals;"color&colon; &num;008000&semi;">anchor point <&sol;span><&sol;strong>&lpar; <strong>this point can be the edge of another view&comma; the edge of the layout&comma; or a guideline<&sol;strong>&rpar; that share the same plane&period; That means a handle on one axis &lpar;such as horizontal&rpar; can&&num;8217&semi;t be connected to a handle on another axis &lpar;such as vertical&rpar;&period;<&sol;li>&NewLine;<li>Each constraint handle can be used for just one constraint&comma; but you can create multiple constraints &lpar;from different views&rpar; to the same anchor point&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Handles<&sol;b><&sol;span><&sol;h3>&NewLine;<p class&equals;"p2">A Constraint is essentially a defined rule for a view which declares it’s positioning and alignment on screen&comma; the ConstraintLayout supports several different types of handles&period;<&sol;p>&NewLine;<h4 class&equals;"p3"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Resize Handle<&sol;b><&sol;span><&sol;h4>&NewLine;<p class&equals;"p2">You can drag the square <strong><span style&equals;"color&colon; &num;008000&semi;">resizing handles<&sol;span><&sol;strong> to resize the element&period; While dragging&comma; the handle changes to an angled corner&period;<&sol;p>&NewLine;<p><img class&equals;" size-full wp-image-1330 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-26-16&period;19&period;21&period;png" alt&equals;"Screenshot 2019-11-26 16&period;19&period;21" width&equals;"204" height&equals;"200" &sol;><&sol;p>&NewLine;<p class&equals;"p1"> Drag corners to resize the image&period; By resizing the image&comma; the width and height are fixed to specific dimensions&period;<&sol;p>&NewLine;<p><img class&equals;" wp-image-1329 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Untitled&period;gif" alt&equals;"Untitled" width&equals;"427" height&equals;"275" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Constraint Handle<&sol;b><&sol;span><&sol;h4>&NewLine;<p class&equals;"p2">The constraint handle can be used to specify the location of the view within the layout&period;<&sol;p>&NewLine;<p>Click a <strong><span style&equals;"color&colon; &num;008000&semi;">constraint handle<&sol;span><&sol;strong>&comma; shown as a circle on each side of an element&comma; and then drag to another constraint handle or to parent boundary to create a constraint&period; The constraint is represented by the zigzag line&period;<&sol;p>&NewLine;<p class&equals;"p2"><img class&equals;"aligncenter wp-image-1331" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-26-18&period;15&period;24-3551820319-1574772394908&period;png" alt&equals;"Screenshot 2019-11-26 18&period;15&period;24" width&equals;"232" height&equals;"173" &sol;><&sol;p>&NewLine;<p class&equals;"p2">For example&comma; these anchors can be used to define that the selected view is always displayed to the right of another view&comma; by a specified margin dp value&period;<&sol;p>&NewLine;<p class&equals;"p2"><img class&equals;" wp-image-1332 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;constraint&lowbar;handle&period;gif" alt&equals;"constraint&lowbar;handle" width&equals;"434" height&equals;"253" &sol;><&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;008000&semi;"><b>Or by setting the desired view attribute in the XML layout&colon;<&sol;b><&sol;span><&sol;p>&NewLine;<pre>app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"&commat;&plus;id&sol;img&lowbar;first"<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Baseline Constraint Handle<&sol;b><&sol;span><&sol;h4>&NewLine;<p class&equals;"p2">By using a <strong><span style&equals;"color&colon; &num;008000&semi;">baseline constraint<&sol;span><&sol;strong>&comma; you can vertically align elements that have text&comma; such as a TextView&comma; EditText&comma; or Button&comma; so that the text baselines are aligned&period;<&sol;p>&NewLine;<p class&equals;"p2">Use baseline constraints to align elements that use different text sizes&period;<&sol;p>&NewLine;<p class&equals;"p2">Baseline constraints are also useful for aligning the text baselines of elements of different sizes&period;<&sol;p>&NewLine;<p><img class&equals;" wp-image-1335 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-26-19&period;09&period;56&period;png" alt&equals;"Screenshot 2019-11-26 19&period;09&period;56" width&equals;"294" height&equals;"158" &sol;><&sol;p>&NewLine;<p class&equals;"p2">This can be set in the layout editor by dragging the baseline anchor of the desired TextView to the baseline of another TextView&colon;<&sol;p>&NewLine;<p><img class&equals;" wp-image-1333 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;baseline&lowbar;constraint&period;gif" alt&equals;"baseline&lowbar;constraint" width&equals;"566" height&equals;"302" &sol;><&sol;p>&NewLine;<p>Now the above two TextViews move together and stay aligned&period;<&sol;p>&NewLine;<p class&equals;"p1"><b>Or by setting the desired view attribute in the XML layout&colon;<&sol;b><&sol;p>&NewLine;<pre>&lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;small&lowbar;text"&NewLine; android&colon;layout&lowbar;width&equals;"114dp"&NewLine; android&colon;layout&lowbar;height&equals;"72dp"&NewLine; android&colon;text&equals;"TextView"&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>app&colon;layout&lowbar;constraintBaseline&lowbar;toBaselineOf&equals;"&commat;&plus;id&sol;big&lowbar;text"<&sol;strong><&sol;span>&NewLine; tools&colon;layout&lowbar;editor&lowbar;absoluteX&equals;"264dp" &sol;&gt&semi;<&sol;pre>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Note&colon; <span style&equals;"color&colon; &num;008000&semi;">A baseline constraint handle can be connected only to another baseline&period;<&sol;span><&sol;b><&sol;span><&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Adding Constraints<&sol;span><&sol;strong><&sol;h3>&NewLine;<p class&equals;"p1">To add a constraint&comma; do the following&colon;<&sol;p>&NewLine;<p>1&period;Drag a view from the <span style&equals;"color&colon; &num;008000&semi;"><b>Palette<&sol;b><&sol;span> window into the editor&period;<br &sol;>&NewLine;When you add a view in a <span class&equals;"s1">ConstraintLayout<&sol;span>&comma; it displays a bounding box with square <span style&equals;"color&colon; &num;008000&semi;"><strong>resizing handles<&sol;strong><&sol;span> on each corner and circular <span style&equals;"color&colon; &num;008000&semi;"><strong>constraint handles<&sol;strong><&sol;span> on each side&period;<&sol;p>&NewLine;<p><img class&equals;" wp-image-1358 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-16&period;51&period;53-3347994767-1574867774762&period;png" alt&equals;"Screenshot 2019-11-27 16&period;51&period;53" width&equals;"277" height&equals;"169" &sol;><&sol;p>&NewLine;<p>2&period;Click the view to select it&period;<&sol;p>&NewLine;<p>3&period;Do one of the following&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>Click a constraint handle and drag it to an available anchor point&period; This point can be the edge of another view&comma; the edge of the layout&comma; or a guideline&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;" wp-image-1357 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;adding&lowbar;constraint&period;gif" alt&equals;"adding&lowbar;constraint" width&equals;"291" height&equals;"409" &sol;><&sol;p>&NewLine;<ul>&NewLine;<li>Click one of the <span style&equals;"color&colon; &num;008000&semi;"><b>Create a connection<&sol;b><&sol;span> buttons in the <span style&equals;"color&colon; &num;008000&semi;"><b>Layout<&sol;b><&sol;span> section of the <span style&equals;"color&colon; &num;0000ff&semi;"><b>Attributes<&sol;b><&sol;span> window&comma; as shown below&colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"aligncenter size-full wp-image-1359" src&equals;"https&colon;&sol;&sol;i0&period;wp&period;com&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-17&period;02&period;36-2644254892-1574867982885&period;png&quest;ssl&equals;1&amp&semi;w&equals;630" alt&equals;"Screenshot 2019-11-27 17&period;02&period;36" width&equals;"315" height&equals;"217" &sol;><&sol;p>&NewLine;<h3 id&equals;"dc2c" class&equals;"kd ke ea ba az kf kg ll ki lm kk ln km lo ko lp kq"><strong><span style&equals;"color&colon; &num;000080&semi;">Deleting Constraints<&sol;span><&sol;strong><&sol;h3>&NewLine;<p id&equals;"377b" class&equals;"ip iq ea ba ir b is kr iu ks iw kt iy ku ja kv jc" data-selectable-paragraph&equals;"">Within the layout editor&comma; you can easily remove constraints from a view&period; This can be done in one of two ways&period;<&sol;p>&NewLine;<h4 id&equals;"22b2" class&equals;"lg ke ea ba az kf lq lr ls lt lu lv lw lx ly lz ma"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Single Constraints<&sol;span><&sol;strong><&sol;h4>&NewLine;<p id&equals;"84c7" class&equals;"ip iq ea ba ir b is kr iu ks iw kt iy ku ja kv jc" data-selectable-paragraph&equals;"">You can remove a single constraint by simply clicking on the anchor point of the constraint that you wish to delete&period;<&sol;p>&NewLine;<ul>&NewLine;<li>Click on a constraint to select it&comma; and then press <strong><span style&equals;"color&colon; &num;008000&semi;">Delete<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li>Press and hold <span style&equals;"color&colon; &num;008000&semi;"><strong>Control<&sol;strong><&sol;span> &lpar;Command on macOS&rpar;&comma; and then click on a constraint anchor&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;" wp-image-1353 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-16&period;35&period;32-3874598248-1574868411977&period;png" alt&equals;"Screenshot 2019-11-27 16&period;35&period;32" width&equals;"285" height&equals;"264" &sol;><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<ul>&NewLine;<li>In the <span style&equals;"color&colon; &num;008000&semi;"><b>Layout<&sol;b><&sol;span> section of the <span style&equals;"color&colon; &num;008000&semi;"><b>Attributes<&sol;b><&sol;span> window&comma; click on a constraint anchor&comma; as shown below&colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"aligncenter size-full wp-image-1354" src&equals;"https&colon;&sol;&sol;i2&period;wp&period;com&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-16&period;38&period;31-2375164943-1574868529222&period;png&quest;ssl&equals;1&amp&semi;w&equals;826" alt&equals;"Screenshot 2019-11-27 16&period;38&period;31" width&equals;"413" height&equals;"249" &sol;><&sol;p>&NewLine;<h4><&sol;h4>&NewLine;<h4 id&equals;"d17d" class&equals;"lg ke ea ba az kf lq lr ls lt lu lv lw lx ly lz ma"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>All Constraints<&sol;strong><&sol;span><&sol;h4>&NewLine;<p id&equals;"76f7" class&equals;"ip iq ea ba ir b is kr iu ks iw kt iy ku ja kv jc" data-selectable-paragraph&equals;"">When a view is selected&comma; right-click and select <span style&equals;"color&colon; &num;008000&semi;"><strong>Clear Constraints of Selection&period; <&sol;strong><&sol;span>Now it will remove <span style&equals;"color&colon; &num;008000&semi;"><strong class&equals;"ir jd">all<&sol;strong><&sol;span> of the constraints that have been set on that view&period;<&sol;p>&NewLine;<p data-selectable-paragraph&equals;""><img class&equals;" wp-image-1356 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-16&period;42&period;41-525242044-1574868627109&period;png" alt&equals;"Screenshot 2019-11-27 16&period;42&period;41" width&equals;"396" height&equals;"255" &sol;><&sol;p>&NewLine;<h3 class&equals;"step-title"><span style&equals;"color&colon; &num;000080&semi;"><strong>The view inspector<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">The <span style&equals;"color&colon; &num;008000&semi;"><b>Attributes<&sol;b><&sol;span> pane shows the view inspector in a square block at the top&period; The view inspector shows the UI element&&num;8217&semi;s constraints and margins&comma; and sliders for adjusting the position of the element along the horizontal and vertical axis&period;<&sol;p>&NewLine;<p><img class&equals;"aligncenter size-full wp-image-1336" src&equals;"https&colon;&sol;&sol;i0&period;wp&period;com&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-26-20&period;23&period;14-657605972-1574869036564&period;png&quest;ssl&equals;1&amp&semi;w&equals;756" alt&equals;"Screenshot 2019-11-26 20&period;23&period;14" width&equals;"378" height&equals;"474" &sol;><&sol;p>&NewLine;<p class&equals;"p1">In the above figure&colon;<&sol;p>&NewLine;<ol class&equals;"ol1">&NewLine;<li class&equals;"li1">Margin values appear on all four sides&period; Change a margin by clicking the value and choosing a different value&period;<&sol;li>&NewLine;<li class&equals;"li1">Inner lines indicate the constrained width and length&period;<&sol;li>&NewLine;<li class&equals;"li1">Use sliders to change the horizontal and vertical constraint bias for elements with opposing constraints&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Position elements with constraint bias<&sol;span><&sol;strong><&sol;h4>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Horizontal Bias<&sol;strong><&sol;span><&sol;h5>&NewLine;<p class&equals;"p1">This allows us to position a view along the horizontal axis using a bias value&comma; this will be relative to it’s constrained position&period;<&sol;p>&NewLine;<p class&equals;"p1"><img class&equals;" wp-image-1337 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;horizontal&lowbar;bias&period;gif" alt&equals;"horizontal&lowbar;bias" width&equals;"381" height&equals;"239" &sol;><&sol;p>&NewLine;<p class&equals;"p1">We can set this using the slider in the layout editor &lpar;seen above&rpar; or using the attribute in our XML like so&colon;<&sol;p>&NewLine;<pre>app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>verticle Bias<&sol;strong><&sol;span><&sol;h5>&NewLine;<p class&equals;"p1">This allows us to position a view along the vertical axis using a bias value&comma; this will be relative to it’s constrained position&period;<&sol;p>&NewLine;<p class&equals;"p1"><img class&equals;" wp-image-1338 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;verticle-&lowbar;bias&period;gif" alt&equals;"verticle &lowbar;bias" width&equals;"355" height&equals;"268" &sol;><&sol;p>&NewLine;<p class&equals;"p1">We can set this using the slider in the layout editor &lpar;seen above&rpar; or using the attribute in our XML like so&colon;<&sol;p>&NewLine;<pre>app&colon;layout&lowbar;constraintVertical&lowbar;bias&equals;"0&period;5"<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Change the element&&num;8217&semi;s layout width and height<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p1">The inner lines within the view inspector let you change the UI element&&num;8217&semi;s layout&lowbar;width and layout&lowbar;height values relative to constraints&period; Clicking an inner line cycles through the following options for vertical and horizontal constraints&colon;<&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Fixed<&sol;b><&sol;span>&colon; Specify the width&sol;height of the element&period;<&sol;p>&NewLine;<p><img class&equals;"" style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;" src&equals;"https&colon;&sol;&sol;codelabs&period;developers&period;google&period;com&sol;codelabs&sol;constraint-layout&sol;img&sol;7fb5ded1aedd7246&period;png" alt&equals;"Screen Shot 2016-05-12 at 5&period;14&period;18 PM&period;png" width&equals;"105" height&equals;"42" &sol;><&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Match Constraints<&sol;b><&sol;span>&colon; Allow the element to occupy all available space to satisfy the constraint&period; &lpar;Note that this is not the same as the match&lowbar;parent value for width or height&comma; which sets the element to occupy all available space of the parent view&period; You shouldn&&num;8217&semi;t use match&lowbar;parent for any view in a ConstraintLayout&period;&rpar; In the XML file&comma; the value 0dp appears in the layout&lowbar;width or layout&lowbar;height attribute for Match Constraints&period;<&sol;p>&NewLine;<p><img class&equals;"" src&equals;"https&colon;&sol;&sol;codelabs&period;developers&period;google&period;com&sol;codelabs&sol;constraint-layout&sol;img&sol;44fff184b08bc2e4&period;png" alt&equals;"Screen Shot 2016-05-12 at 5&period;13&period;12 PM&period;png" width&equals;"110" height&equals;"47" &sol;><&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Wrap Content<&sol;b><&sol;span>&colon; Expand the element as needed to fit its content&period;<&sol;p>&NewLine;<p><img class&equals;"" src&equals;"https&colon;&sol;&sol;codelabs&period;developers&period;google&period;com&sol;codelabs&sol;constraint-layout&sol;img&sol;d066cfdb50a039ba&period;png" alt&equals;"Screen Shot 2016-05-12 at 5&period;14&period;04 PM&period;png" width&equals;"106" height&equals;"41" &sol;><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>AutoConnect Tool<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><img class&equals;"alignnone wp-image-1349" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-13&period;46&period;25&period;png" alt&equals;"Screenshot 2019-11-27 13&period;46&period;25" width&equals;"473" height&equals;"71" &sol;><&sol;p>&NewLine;<p class&equals;"p1"><strong><span style&equals;"color&colon; &num;008000&semi;">Auto-connect<&sol;span><&sol;strong> allows you to place a view inside the layout editor and have it automatically calculate and set the constraints for you&period;<&sol;p>&NewLine;<p>The Autoconnect tool <img src&equals;"https&colon;&sol;&sol;codelabs&period;developers&period;google&period;com&sol;codelabs&sol;constraint-layout&sol;img&sol;dd3846009e393c48&period;png" alt&equals;"Screen Shot 2016-05-12 at 8&period;30&period;36 PM&period;png" &sol;> is located in the toolbar&period; It is enabled by default&period; For this exercise&comma; ensure that the tool is enabled&period;<&sol;p>&NewLine;<p class&equals;"p1">In the blueprint or the preview&comma; click&comma; then drag any of the views near the corners of the parent view or towards the center of the layout until dotted guidelines appear&period; As you do so&comma; Android Studio will automatically create new constraints for you&period;<&sol;p>&NewLine;<p><img class&equals;" wp-image-1342 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Autocontent&lowbar;tool&period;gif" alt&equals;"Autocontent&lowbar;tool" width&equals;"505" height&equals;"404" &sol;><&sol;p>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Manual Constraints<&sol;b><&sol;span><&sol;h3>&NewLine;<p class&equals;"p2">It’s possible to disable auto-connect if you wish to manually set the constraints within the layout editor&period; Personally&comma; this gives you much more control over the constraints and saves you time deleting the constraints you don’t need that were automatically set&period;<&sol;p>&NewLine;<p><img class&equals;" wp-image-1343 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;manual&lowbar;constraint&period;gif" alt&equals;"manual&lowbar;constraint" width&equals;"528" height&equals;"416" &sol;><&sol;p>&NewLine;<p class&equals;"p2">To use manual constraints you need to disable auto-connect&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1348" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-13&period;40&period;02&period;png" alt&equals;"Screenshot 2019-11-27 13&period;40&period;02" width&equals;"500" height&equals;"86" &sol;><&sol;p>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Infer Constraints<&sol;b><&sol;span><&sol;h3>&NewLine;<p><img class&equals;"alignnone wp-image-1347" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-13&period;40&period;39&period;png" alt&equals;"Screenshot 2019-11-27 13&period;40&period;39" width&equals;"482" height&equals;"78" &sol;><&sol;p>&NewLine;<p class&equals;"p2">The Infer Constraints tool infers&comma; or figures out&comma; the constraints you need to match a rough layout of elements&period; It works by taking into account the positions and sizes of the elements&period; Drag elements to the layout in the positions you want them&comma; and use the Infer Constraints tool to automatically create the constraint connections&period;<&sol;p>&NewLine;<p>Before we click on <strong><span style&equals;"color&colon; &num;008000&semi;">infer constraints tool<&sol;span><&sol;strong> the layout look like this&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1344" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-13&period;17&period;48&period;png" alt&equals;"Screenshot 2019-11-27 13&period;17&period;48" width&equals;"625" height&equals;"496" &sol;><&sol;p>&NewLine;<p>Click the<strong><span style&equals;"color&colon; &num;008000&semi;"> Infer Constraints tool<&sol;span><&sol;strong>&period; The Layout Editor adds constraints to all of the unconstrained elements in the layout&period; The resulting layout should look like the following&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1345" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-13&period;18&period;59&period;png" alt&equals;"Screenshot 2019-11-27 13&period;18&period;59" width&equals;"630" height&equals;"502" &sol;><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>What&&num;8217&semi;s the difference between Inference and Autoconnect&quest;<&sol;strong><&sol;span><&sol;p>&NewLine;<p>The Infer Constraints <img src&equals;"https&colon;&sol;&sol;codelabs&period;developers&period;google&period;com&sol;codelabs&sol;constraint-layout&sol;img&sol;762af6efa02c9471&period;png" &sol;> tool calculates and sets constraints for all of the elements in a layout&comma; rather than just the selected element&period; It bases its calculations on inferred relationships between the elements&period;<&sol;p>&NewLine;<p>The Autoconnect <img src&equals;"https&colon;&sol;&sol;codelabs&period;developers&period;google&period;com&sol;codelabs&sol;constraint-layout&sol;img&sol;dd3846009e393c48&period;png" alt&equals;"Screen Shot 2016-05-12 at 8&period;30&period;36 PM&period;png" &sol;> tool creates constraint connections for a selected element to the element&&num;8217&semi;s parent&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Default Margin <&sol;strong><&sol;span><&sol;h3>&NewLine;<p>To create vertically-distributed constraints&comma; you simply connect constraints that have the same margins&period; A trick to do this quickly is to use the Default Margin tool&period;<&sol;p>&NewLine;<p>click <span style&equals;"color&colon; &num;008000&semi;"><strong>Margin<&sol;strong><&sol;span> <img class&equals;"inline-icon" src&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;images&sol;training&sol;constraint-layout&sol;constraint-layout-margin-icon-2x&period;png" alt&equals;"" width&equals;"53" height&equals;"33" &sol;>  in the toolbar to select the default margin for each view that you add to the layout&period;<&sol;p>&NewLine;<p><img class&equals;" wp-image-1351 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-15&period;55&period;02&period;png" alt&equals;"Screenshot 2019-11-27 15&period;55&period;02" width&equals;"456" height&equals;"65" &sol;><&sol;p>&NewLine;<p>After setting the default margin to 60dp when we constrain a view with another view or with parent layout then it will be separated by 60dp&period;<&sol;p>&NewLine;<p><img class&equals;" wp-image-1350 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;default&lowbar;margin&period;gif" alt&equals;"default&lowbar;margin" width&equals;"548" height&equals;"437" &sol;><&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Clear All Constraints<&sol;span><&sol;strong><&sol;h3>&NewLine;<p><img class&equals;"alignnone wp-image-1346" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-27-13&period;34&period;37&period;png" alt&equals;"Screenshot 2019-11-27 13&period;34&period;37" width&equals;"484" height&equals;"94" &sol;><&sol;p>&NewLine;<p class&equals;"p1">If the constraints are messed up&comma; you can clear all constraints and restart from scratch&period; To continue&comma; clear your current constraints with the <span style&equals;"color&colon; &num;008000&semi;"><b>Clear All Constraints<&sol;b><&sol;span> button&period;<&sol;p>&NewLine;<h3 class&equals;"step-title"><strong><span style&equals;"color&colon; &num;000080&semi;">Use ratios to size elements<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>You can quickly resize elements by aspect ratio if at least one of the element&&num;8217&semi;s dimensions is set to match constraints&period;<&sol;p>&NewLine;<p>By using ratios you can ensure your designs stay perfect while allowing images to be resized on different device screens&period;<&sol;p>&NewLine;<p>You can see the entire action and result in the following animated figure&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1352" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;using&lowbar;ratio&period;gif" alt&equals;"using&lowbar;ratio" width&equals;"714" height&equals;"376" &sol;><&sol;p>&NewLine;<p>I hope this article will help you in understanding <span style&equals;"color&colon; &num;008000&semi;"><strong>ConstraintLayout<&sol;strong><&sol;span>&comma; it’s capabilities and how we can use it to create more efficient layouts within our applications&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p data-selectable-paragraph&equals;"">&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version