Site icon C1CTech

Android Creating Complex Layouts using ConstraintLayout

<p>Android <strong><span style&equals;"color&colon; &num;008000&semi;">ConstraintLayout<&sol;span><&sol;strong> helps you to create large and complex layouts with a flat view hierarchy without nested view groups&period; This helps to create high performing layouts&period; Android Studio&&num;8217&semi;s Layout Editor makes it really easy to define the constraints for the views&period;<&sol;p>&NewLine;<p class&equals;"p1">ConstraintLayout&comma; which is now the default layout in Android Studio&comma; gives you many ways to place objects&period; You can constrain them to their container&comma; to each other or to guidelines&period;<&sol;p>&NewLine;<p>In this ConstraintLayout tutorial&comma; you’ll learn various types of constraints that you can use to build a UI with constraint layout is explained in detail&period;<&sol;p>&NewLine;<p>There are currently various types of constraints that you can use&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Relative Positioning<&sol;span><&sol;strong><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Center Positioning<&sol;span><&sol;strong><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Circular Positioning<&sol;span><&sol;strong><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Dimension Constraint<&sol;span><&sol;strong><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Chains<&sol;span><&sol;strong><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Virtual Helper Objects<&sol;span><&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Relative Positioning<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Relative positioning<&sol;strong><&sol;span> is to constraint a given side of the widget relative to a side of any other widget or to the parent ViewGroup either horizontally or vertically&period;<&sol;p>&NewLine;<p>You can constrain a widget on the horizontal and vertical axis&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Horizontal Axis<&sol;span><&sol;strong>&colon; left&comma; right&comma; start and end sides<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Vertical Axis<&sol;span><&sol;strong>&colon; top&comma; bottom sides and text baseline<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Given below is the basic format of the attribute for relative positioning&colon;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">app&colon; layout&lowbar;constraint<span style&equals;"color&colon; &num;0000ff&semi;">&lbrack;Source Side&rsqb;<&sol;span>&lowbar;to<span style&equals;"color&colon; &num;0000ff&semi;">&lbrack;Target Side&rsqb;<&sol;span>Of&equals;&&num;8221&semi;<span style&equals;"color&colon; &num;0000ff&semi;">&lbrack;Target id or parent&rsqb;<&sol;span>&&num;8220&semi;<&sol;span><&sol;strong><&sol;p>&NewLine;<p class&equals;"p1">Here is the list of available Relative constraints &colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintLeft&lowbar;toLeftOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintLeft&lowbar;toRightOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintRight&lowbar;toLeftOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintRight&lowbar;toRightOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintTop&lowbar;toTopOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintTop&lowbar;toBottomOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintBottom&lowbar;toTopOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintBottom&lowbar;toBottomOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintBaseline&lowbar;toBaselineOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintStart&lowbar;toEndOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintStart&lowbar;toStartOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintEnd&lowbar;toStartOf<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintEnd&lowbar;toEndOf<&sol;b><&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong><span style&equals;"color&colon; &num;0000ff&semi;">For example<&sol;span>&comma;<&sol;strong><&sol;span> in order to position button B to the right of button A we will constrain the left anchor point of button B to the right anchor point of button A with margin of 60dp&period;<&sol;p>&NewLine;<p><img class&equals;"wp-image-1368 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;relative&lowbar;positioning&period;gif" alt&equals;"relative&lowbar;positioning" width&equals;"486" height&equals;"250" &sol;><&sol;p>&NewLine;<p>This is achieved using the attribute&colon;<&sol;p>&NewLine;<pre>app&colon; layout&lowbar;constraintLeft&lowbar;toRightOf &equals; "&commat;&plus;id&sol;buttonA"<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Center Positioning<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Center positioning<&sol;span><&sol;strong> constraint is used to achieve the same behavior that we achieve using the below attributes of RelativeLayout&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">layout&lowbar;centerInParent<&sol;span><&sol;strong><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">layout&lowbar;centerHorizontal<&sol;span><&sol;strong><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">layout&lowbar;centerVerticle<&sol;span><&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>For centering horizontally or vertically we need to relatively constraint the widget between two <strong><span style&equals;"color&colon; &num;000000&semi;">targets<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>The <span style&equals;"color&colon; &num;008000&semi;"><strong>Target<&sol;strong><&sol;span> can either be widget&&num;8217&semi;s anchor point or the parent&&num;8217&semi;s anchor point&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">For example<&sol;span><&sol;strong>&comma; below you can see we want <span style&equals;"color&colon; &num;008000&semi;"><strong>ButtonA<&sol;strong><&sol;span> to be at the center of the parent&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">ButtonB<&sol;span><&sol;strong> to be at the left of ButtonA and horizontally-centered and <span style&equals;"color&colon; &num;008000&semi;"><strong>ButtonC<&sol;strong><&sol;span> to be below ButtonA and vertically&lowbar;centered&period;<&sol;p>&NewLine;<p><img class&equals;"wp-image-1369 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;center&lowbar;positioning&period;gif" alt&equals;"center&lowbar;positioning" width&equals;"291" height&equals;"470" &sol;><&sol;p>&NewLine;<p>You can also place a view at <span style&equals;"color&colon; &num;008000&semi;"><strong>horizontally-centered<&sol;strong><&sol;span> or <strong><span style&equals;"color&colon; &num;008000&semi;">vertically-centered<&sol;span><&sol;strong> instead of dragging an anchor point and connecting it with another anchor point as we have done above by doing this&colon;<&sol;p>&NewLine;<p><img class&equals;"wp-image-1370 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-29-11&period;57&period;20&period;png" alt&equals;"Screenshot 2019-11-29 11&period;57&period;20" width&equals;"464" height&equals;"391" &sol;><&sol;p>&NewLine;<p>There might be a requirement where you don&&num;8217&semi;t want the element to be precisely at the center instead you want it to be some x percent to the <span style&equals;"color&colon; &num;008000&semi;"><strong>left or right&sol;top or bottom<&sol;strong><&sol;span> from the center in that case an interesting constraint comes into play known as <span style&equals;"color&colon; &num;0000ff&semi;"><strong>bias<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Bias<&sol;span><&sol;strong> favor one side over another using the bias attributes&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintHorizontal&lowbar;bias<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintVertical&lowbar;bias<&sol;b><&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">For example&comma;<&sol;span><&sol;strong> the following will make the top side with a 30&percnt; bias instead of the default 50&percnt;&comma; such that the top side will be shorter&comma; with the widget leaning more toward the top side &colon;<&sol;p>&NewLine;<p><img class&equals;"wp-image-1371 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;center&lowbar;positioning&lowbar;with&lowbar;bias&period;gif" alt&equals;"center&lowbar;positioning&lowbar;with&lowbar;bias" width&equals;"550" height&equals;"386" &sol;><&sol;p>&NewLine;<p>Or you can simply add the attribute in buttonA&colon;<&sol;p>&NewLine;<pre>app&colon;layout&lowbar;constraintVertical&lowbar;bias&equals;"0&period;3"<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Circular Positioning<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Circular Positioning<&sol;span><&sol;strong> constraint allows us to constrain the center of a widget relative to the center of another widget&comma; at an angle and a distance&period;<&sol;p>&NewLine;<p>Using this constraint we can align a widget on a circle&period;<&sol;p>&NewLine;<h3>              <img class&equals;"alignnone size-full wp-image-1373" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;circular&lowbar;positioning1&period;png" alt&equals;"circular&lowbar;positioning1" width&equals;"225" height&equals;"225" &sol;><img class&equals;"alignnone size-full wp-image-1374" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;circular&lowbar;positioning2&period;png" alt&equals;"circular&lowbar;positioning2" width&equals;"225" height&equals;"225" &sol;><&sol;h3>&NewLine;<p>Following are the attributes for circular positioning constraint&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintCircle<&sol;b><&sol;span> &equals;&&num;8221&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">&lbrack;references another widget id&rsqb;<&sol;span><&sol;strong>&&num;8220&semi;<&sol;li>&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"s2"><b>layout&lowbar;constraintCircleRadius<&sol;b><&sol;span> &equals;&&num;8221&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">&lbrack;<&sol;span><&sol;strong><&sol;span><strong><span style&equals;"color&colon; &num;008000&semi;">the distance to the other widget center in dp&rsqb;<&sol;span><&sol;strong>&&num;8220&semi;<&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintCircleAngle<&sol;b><&sol;span> &equals;&&num;8221&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">&lbrack;the angle the widget should be at &lpar;in degrees&comma; from 0 to 360&rpar; of type int&rsqb;<&sol;span><&sol;strong>&&num;8220&semi;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>For example&comma;<&sol;strong><&sol;span> we have <span style&equals;"color&colon; &num;008000&semi;"><strong>buttonA<&sol;strong><&sol;span>&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>buttonB<&sol;strong><&sol;span>&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>buttonC<&sol;strong><&sol;span>&comma; and <span style&equals;"color&colon; &num;008000&semi;"><strong>buttonD<&sol;strong><&sol;span>&period; Now we want buttonB&comma; buttonC&comma; and buttonD to be arranged around buttonA&period;<&sol;p>&NewLine;<p><img class&equals;"wp-image-1376 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-29-14&period;32&period;18-546521047-1575289605841&period;png" alt&equals;"Screenshot 2019-11-29 14&period;32&period;18" width&equals;"356" height&equals;"360" &sol;><&sol;p>&NewLine;<p>To achieve the above UI we have to write the following code in our xml file&colon;<&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;linearLayout"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;buttonA"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&commat;drawable&sol;bg&lowbar;redcolor"&NewLine; android&colon;text&equals;"Button A"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textSize&equals;"18sp"&NewLine; tools&colon;layout&lowbar;editor&lowbar;absoluteX&equals;"243dp"&NewLine; tools&colon;layout&lowbar;editor&lowbar;absoluteY&equals;"290dp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;buttonB"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&commat;drawable&sol;bg&lowbar;redcolor"&NewLine; android&colon;text&equals;"Button B"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textSize&equals;"18sp"&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">app&colon;layout&lowbar;constraintCircle&equals;"&commat;id&sol;buttonA"<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> app&colon;layout&lowbar;constraintCircleAngle&equals;"315"<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> app&colon;layout&lowbar;constraintCircleRadius&equals;"200dp"<&sol;span><&sol;strong>&NewLine; tools&colon;layout&lowbar;editor&lowbar;absoluteX&equals;"252dp"&NewLine; tools&colon;layout&lowbar;editor&lowbar;absoluteY&equals;"192dp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;buttonC"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&commat;drawable&sol;bg&lowbar;redcolor"&NewLine; android&colon;text&equals;"Button C"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textSize&equals;"18sp"&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>app&colon;layout&lowbar;constraintCircle&equals;"&commat;id&sol;buttonA"<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> app&colon;layout&lowbar;constraintCircleAngle&equals;"360"<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> app&colon;layout&lowbar;constraintCircleRadius&equals;"200dp"<&sol;strong><&sol;span>&NewLine; tools&colon;layout&lowbar;editor&lowbar;absoluteX&equals;"225dp"&NewLine; tools&colon;layout&lowbar;editor&lowbar;absoluteY&equals;"341dp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;buttonD"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&commat;drawable&sol;bg&lowbar;redcolor"&NewLine; android&colon;text&equals;"Button D"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textSize&equals;"18sp"&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">app&colon;layout&lowbar;constraintCircle&equals;"&commat;id&sol;buttonA"<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> app&colon;layout&lowbar;constraintCircleAngle&equals;"45"<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> app&colon;layout&lowbar;constraintCircleRadius&equals;"200dp"<&sol;span><&sol;strong>&NewLine; tools&colon;layout&lowbar;editor&lowbar;absoluteX&equals;"313dp"&NewLine; tools&colon;layout&lowbar;editor&lowbar;absoluteY&equals;"637dp" &sol;&gt&semi;&NewLine; &NewLine;&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Dimension Constraints<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">The dimension of the widgets can be specified by setting the <span class&equals;"s1" style&equals;"color&colon; &num;008000&semi;"><b>android&colon;layout&lowbar;width<&sol;b><&sol;span> and <span class&equals;"s1" style&equals;"color&colon; &num;008000&semi;"><b>android&colon;layout&lowbar;height<&sol;b><&sol;span> attributes in 3 different ways&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">Hardcode value in <span style&equals;"color&colon; &num;0000ff&semi;"><strong>dp<&sol;strong><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s1" style&equals;"color&colon; &num;0000ff&semi;"><b>WRAP&lowbar;CONTENT<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s1" style&equals;"color&colon; &num;0000ff&semi;"><b>MATCH&lowbar;CONSTRAINT<&sol;b><&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;800080&semi;"><strong>Note<&sol;strong><&sol;span> &colon; <strong>In constraintLayout use of MATCH&lowbar;PARENT is discouraged because MATCH&lowbar;PARENT does not take constraints applied on the widget into consideration&period;<&sol;strong><&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">WRAP&lowbar;CONTENT<&sol;span><&sol;strong><&sol;p>&NewLine;<p>The widget with its dimension set to WRAP&lowbar;CONTENT will only take the space that is needed&period;<&sol;p>&NewLine;<p><img class&equals;"wp-image-1380 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-29-15&period;29&period;54&period;png" alt&equals;"Screenshot 2019-11-29 15&period;29&period;54" width&equals;"419" height&equals;"191" &sol;><&sol;p>&NewLine;<p>But if you only set the dimension to WRAP&lowbar;CONTENT then we might not get the desired result&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>For example<&sol;strong><&sol;span>&comma; I have a TextView whose dimension is set to WRAP&lowbar;CONTENT and its right anchor point is constrained with the left anchor point of a button&period; Below you can see TextView is not respecting the constrained applied on it&period;<&sol;p>&NewLine;<p><img class&equals;"size-full wp-image-1379 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-29-15&period;29&period;01&period;png" alt&equals;"Screenshot 2019-11-29 15&period;29&period;01" width&equals;"460" height&equals;"180" &sol;><&sol;p>&NewLine;<p>This was the issue encounters in the earlier version of ConstrainLayout ie&period; before 1&period;1&period; In 1&period;1 a new constraint was introduced to handle this issue&period; The attributes are&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>app&colon;layout&lowbar;constrainedWidth&equals;”true&vert;false”<&sol;b><&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>app&colon;layout&lowbar;constrainedHeight&equals;”true&vert;false”<&sol;b><&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Now after setting the below attribute in the TextView&comma; you can see the expected output&colon;<&sol;p>&NewLine;<pre>app&colon;layout&lowbar;constrainedWidth&equals;”true”<&sol;pre>&NewLine;<p><img class&equals;"size-full wp-image-1381 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-29-15&period;46&period;57&period;png" alt&equals;"Screenshot 2019-11-29 15&period;46&period;57" width&equals;"456" height&equals;"176" &sol;><&sol;p>&NewLine;<p>There are some already known modifiers that are used frequently in conjunction with WRAP&lowbar;CONTENT and those modifiers are valid in ConstraintLayout as well&period; These modifiers are&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>android&colon;minWidth<&sol;b><&sol;span> set the minimum width for the layout<&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>android&colon;minHeight<&sol;b><&sol;span> set the minimum height for the layout<&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>android&colon;maxWidth<&sol;b><&sol;span> set the maximum width for the layout<&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>android&colon;maxHeight<&sol;b><&sol;span> set the maximum height for the layout<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;800080&semi;">Note<&sol;span><&sol;strong>&colon; <strong>These modifiers are only valid with WRAP&lowbar;CONTENT and not with MATCH&lowbar;CONSTRAINT&period;<&sol;strong><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MATCH&lowbar;CONSTRAINT<&sol;strong><&sol;span><&sol;p>&NewLine;<p>The widget that specifies the dimension to be MATCH&lowbar;CONSTRAINT will take all the available space&period;<&sol;p>&NewLine;<p>We use odp to represent MATCH&lowbar;CONSTRAINT in Constraintlayout&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>For example<&sol;strong><&sol;span>&comma; below you can see as we change the textview margin to MATCH&lowbar;CONSTRAINT it will occupy all available space after leaving margins&period;<&sol;p>&NewLine;<p><img class&equals;"size-full wp-image-1385 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;match&lowbar;constraint&period;gif" alt&equals;"match&lowbar;constraint" width&equals;"1000" height&equals;"268" &sol;><&sol;p>&NewLine;<p>Along with MATCH&lowbar;CONSTRAINT several modifiers are also available&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintWidth&lowbar;min<&sol;b><&sol;span> and <span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintHeight&lowbar;min<&sol;b><&sol;span> &colon; will set the minimum size for this dimension<&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintWidth&lowbar;max<&sol;b><&sol;span> and <span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintHeight&lowbar;max<&sol;b><&sol;span> &colon; will set the maximum size for this dimension<&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintWidth&lowbar;percent<&sol;b><&sol;span> and <span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintHeight&lowbar;percent<&sol;b><&sol;span> &colon; will set the size of this dimension as a percentage of the parent<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>To specify the width as the percent of parent viewgroup width&period; Before applying percent width we need to make sure that the anchor points on both the sides are constrained to the respective sides of the parent viewgroup&period;<&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;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">For example<&sol;span><&sol;strong>&comma; below you can see button width is 50&percnt; of the parent viewgroup width after adding the attribute <span class&equals;"s2"><span style&equals;"color&colon; &num;0000ff&semi;"><b>layout&lowbar;constraintWidth&lowbar;percent <&sol;b><&sol;span>in button&period;<&sol;span><&sol;p>&NewLine;<pre>&lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;button12"&NewLine; android&colon;layout&lowbar;width&equals;"0dp"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"Button"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>app&colon;layout&lowbar;constraintWidth&lowbar;percent&equals;"0&period;5"<&sol;strong><&sol;span>&NewLine; tools&colon;layout&lowbar;editor&lowbar;absoluteY&equals;"335dp" &sol;&gt&semi;<&sol;pre>&NewLine;<h3><img class&equals;"alignnone size-full wp-image-1387" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-29-17&period;21&period;50&period;png" alt&equals;"Screenshot 2019-11-29 17&period;21&period;50" width&equals;"1094" height&equals;"192" &sol;><&sol;h3>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Ratio<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>In ConstraintLayout you can define one dimension of a widget as a ratio of the other one&period;<&sol;p>&NewLine;<p>The attribute for specifying the ratio is<&sol;p>&NewLine;<p class&equals;"p1"><span class&equals;"s1" style&equals;"color&colon; &num;008000&semi;"><b>app&colon;layout&lowbar;constraintDimensionRatio&equals;&&num;8221&semi;&lbrack;ratio&rsqb;&&num;8221&semi;<&sol;b><&sol;span><&sol;p>&NewLine;<p>while applying ratio constraint we need to keep the following points in mind&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>Setting one or both view&&num;8217&semi;s dimension to MATCH&lowbar;CONSTRAINT&lpar;0dp&rpar;&period;<&sol;li>&NewLine;<li>Opposite sides of the widget needs to be constrained&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">For example<&sol;span><&sol;strong>&comma; if we set a ratio of an ImageView to 1&colon;1 then it will set the height of the image to be the same as its width&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1388" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-29-17&period;47&period;14&period;png" alt&equals;"Screenshot 2019-11-29 17&period;47&period;14" width&equals;"390" height&equals;"616" &sol;><&sol;p>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Chains<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>A <strong><span style&equals;"color&colon; &num;008000&semi;">chain<&sol;span><&sol;strong> is a group of views that are linked to each other with bi-directional position constraints&period;<&sol;p>&NewLine;<p>The views within a chain can be distributed either vertically or horizontally&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Chain Style<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p1">When setting the attribute <span style&equals;"color&colon; &num;008000&semi;"><span class&equals;"s1"><b>layout&lowbar;constraintHorizontal&lowbar;chainStyle<&sol;b><&sol;span> <&sol;span> or <span class&equals;"s1" style&equals;"color&colon; &num;008000&semi;"><b>layout&lowbar;constraintVertical&lowbar;chainStyle<&sol;b><&sol;span> on the first element of a chain&comma; the behavior of the chain will change according to the specified style &lpar;default is <span class&equals;"s1" style&equals;"color&colon; &num;008000&semi;"><b>CHAIN&lowbar;SPREAD<&sol;b><&sol;span>&rpar;&period;<&sol;p>&NewLine;<p>Chains can be styled in one of the following ways&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><b><span style&equals;"color&colon; &num;0000ff&semi;">Spread<&sol;span>&colon;<&sol;b> The views are evenly distributed &lpar;after margins are accounted for&rpar;&period; This is the default&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;" wp-image-1366 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-28-18&period;03&period;37-1432640571-1575295446127&period;png" alt&equals;"Screenshot 2019-11-28 18&period;03&period;37" width&equals;"568" height&equals;"107" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><b><span style&equals;"color&colon; &num;0000ff&semi;">Spread inside<&sol;span>&colon;<&sol;b> The first and last view are affixed to the constraints on each end of the chain and the rest are evenly distributed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;" wp-image-1364 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-28-18&period;02&period;35-64379506-1575295414880&period;png" alt&equals;"Screenshot 2019-11-28 18&period;02&period;35" width&equals;"570" height&equals;"104" &sol;><&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><b><span style&equals;"color&colon; &num;0000ff&semi;">Weighted<&sol;span>&colon;<&sol;b> When the chain is set to either <span style&equals;"color&colon; &num;008000&semi;"><b>spread<&sol;b><&sol;span> or <span style&equals;"color&colon; &num;008000&semi;"><b>spread inside<&sol;b><&sol;span>&comma; you can fill the remaining space by setting one or more views to &&num;8220&semi;match constraints&&num;8221&semi; &lpar;<span class&equals;"s2">0dp<&sol;span>&rpar;&period; By default&comma; space is evenly distributed between each view that&&num;8217&semi;s set to &&num;8220&semi;match constraints&comma;&&num;8221&semi; but you can assign a weight of importance to each view using the attributes&colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;"><span class&equals;"s2">            layout&lowbar;constraintHorizontal&lowbar;weight<&sol;span><&sol;span><&sol;strong><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s2">            layout&lowbar;constraintVertical&lowbar;weight<&sol;span><&sol;strong><&sol;span><&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">For example&comma;<&sol;span> <&sol;strong>on a chain containing three buttons using width <span style&equals;"color&colon; &num;008000&semi;"><strong>MATCH&lowbar;CONSTRAINT<&sol;strong><&sol;span>&comma; with the first button using a weight of 1&comma; second&comma; a weight of 2 and third with a weight of 3&comma; the space occupied by the third button will be thrice that of the first button and the space occupied by the second button will be twice that of the first button&period;<&sol;p>&NewLine;<p><img class&equals;"wp-image-1367 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-28-18&period;25&period;16&period;png" alt&equals;"Screenshot 2019-11-28 18&period;25&period;16" width&equals;"585" height&equals;"104" &sol;><&sol;p>&NewLine;<p style&equals;"padding-left&colon; 40px&semi;">If you&&num;8217&semi;re familiar with <span class&equals;"s2">layout&lowbar;weight<&sol;span> in a <a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;guide&sol;topics&sol;ui&sol;layout&sol;linear"><span class&equals;"s3"><strong><span style&equals;"color&colon; &num;008000&semi;">linear<&sol;span><&sol;strong> <strong><span style&equals;"color&colon; &num;008000&semi;">layout<&sol;span><&sol;strong><&sol;span><&sol;a><strong><span style&equals;"color&colon; &num;008000&semi;">&comma;<&sol;span><&sol;strong> this works the same way&period; So the view with the highest weight value gets the most amount of space&semi; views that have the same weight get the same amount of space&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><b><span style&equals;"color&colon; &num;0000ff&semi;">Packed<&sol;span>&colon;<&sol;b> The views are packed together &lpar;after margins are accounted for&rpar;&period; You can then adjust the whole chain&&num;8217&semi;s bias &lpar;left&sol;right or up&sol;down&rpar; by changing the chain&&num;8217&semi;s head view bias&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"wp-image-1365 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-28-18&period;03&period;09&period;png" alt&equals;"Screenshot 2019-11-28 18&period;03&period;09" width&equals;"610" height&equals;"156" &sol;><&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Chain Heads<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>Chains are controlled by attributes set on the first element of the chain &lpar;the &&num;8220&semi;head&&num;8221&semi; of the chain&rpar;&period;<&sol;p>&NewLine;<p>The head is the left-most view in a horizontal chain and the top-most view in a vertical chain&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Creating a Chain<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p1">To create a chain&comma; select all of the views to be included in the chain&comma; right-click one of the views&comma; select <span style&equals;"color&colon; &num;008000&semi;"><b>Chains<&sol;b><&sol;span> and then select either <span style&equals;"color&colon; &num;008000&semi;"><b>Center Horizontally<&sol;b><&sol;span> or <span style&equals;"color&colon; &num;008000&semi;"><b>Center Vertically<&sol;b><&sol;span>&comma; as shown below&colon;<&sol;p>&NewLine;<p><img class&equals;"wp-image-1362 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;horizontal&lowbar;chain&lowbar;new&period;gif" alt&equals;"horizontal&lowbar;chain&lowbar;new" width&equals;"537" height&equals;"387" &sol;><&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Switch between Chain Styles<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>You can switch between <span style&equals;"color&colon; &num;008000&semi;"><b>spread<&sol;b><&sol;span>&comma; <span style&equals;"color&colon; &num;008000&semi;"><b>spread inside<&sol;b><&sol;span>&comma; and <span style&equals;"color&colon; &num;008000&semi;"><b>packed<&sol;b><&sol;span> chain by selecting any view in the chain&comma; right-click and then select <strong><span style&equals;"color&colon; &num;008000&semi;">Cycle Chain mode<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Virtual Helper Objects<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Virtual Helper Objects<&sol;span><&sol;strong> are views that are not displayed on the device and are only used for layout purposes&period;<&sol;p>&NewLine;<p>The three basic Virtual Helper Objects are&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Guideline<&sol;strong><&sol;span><&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Barrier<&sol;strong><&sol;span><&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Group<&sol;strong><&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Guideline<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Guideline<&sol;strong><&sol;span> is a helper view that is used to align other views&period;<&sol;p>&NewLine;<p>The position of all the views that are aligned to a guideline can be changed at once or allowing reactive layout behavior by using percent positioning&period;<&sol;p>&NewLine;<p class&equals;"p1">A Guideline can be either horizontal or vertical&colon;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Vertical Guideline<&sol;span><&sol;strong><&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Width<&sol;span><&sol;strong>&equals;0     <strong><span style&equals;"color&colon; &num;008000&semi;">Height<&sol;span><&sol;strong> &equals; Height of parent<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Horizontal Guidelines <&sol;strong><&sol;span><&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Height<&sol;span><&sol;strong>&equals;0    <strong><span style&equals;"color&colon; &num;008000&semi;">Width<&sol;span><&sol;strong> &equals; Width of parent<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Creating Guideline<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>To create a guideline&comma; click <span style&equals;"color&colon; &num;008000&semi;"><strong>Guidelines<&sol;strong><&sol;span> <img class&equals;"inline-icon" src&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;studio&sol;images&sol;buttons&sol;layout-editor-guidelines&period;png" alt&equals;"" width&equals;"23" height&equals;"23" &sol;> in the toolbar&comma; and then click either <span style&equals;"color&colon; &num;008000&semi;"><strong>Add Vertical Guideline<&sol;strong> <span style&equals;"color&colon; &num;000000&semi;">or<&sol;span> <strong>Add Horizontal Guideline<&sol;strong>&period;<&sol;span><&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Switch between Guidelines<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>To switch between different types of guidelines we have to click the button available at the starting point of the guideline&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>For example&comma;<&sol;strong><&sol;span> in the below figure&comma; I have set the guideline 112dp away from the start of the screen&period; Now if I click on <strong><span style&equals;"color&colon; &num;008000&semi;">Cycle Guideline<&sol;span><&sol;strong> button it positions itself 489dp away from the end of the parent layout and on clicking it third time it positions itself at 18&percnt; of the parent width&period;<&sol;p>&NewLine;<p><img class&equals;"wp-image-1389 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;cycle&lowbar;guidelines&period;gif" alt&equals;"cycle&lowbar;guidelines" width&equals;"452" height&equals;"488" &sol;><&sol;p>&NewLine;<p class&equals;"p1">Positioning a Guideline is possible in three different ways&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span class&equals;"s2"><strong><span style&equals;"color&colon; &num;0000ff&semi;">layout&lowbar;constraintGuide&lowbar;begin<&sol;span><&sol;strong> &colon; <&sol;span>specifying a fixed distance from the left or the top of a layout<&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2"><strong><span style&equals;"color&colon; &num;0000ff&semi;">layout&lowbar;constraintGuide&lowbar;end<&sol;span><&sol;strong>&colon; specifying a fixed distance from the right or the bottom of a layout <&sol;span><&sol;li>&NewLine;<li class&equals;"li1"><span class&equals;"s2"><strong><span style&equals;"color&colon; &num;0000ff&semi;">layout&lowbar;constraintGuide&lowbar;percent<&sol;span><&sol;strong>&colon; specifying a percentage of the width or the height of a layout <&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>In the below figure&comma; all the four TextViews are constrained with the guideline and if I move the guideline all the TextViews that are constrained with the guideline are moving along with it&period;<&sol;p>&NewLine;<p><img class&equals;"wp-image-1390 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;guideline&period;gif" alt&equals;"guideline" width&equals;"554" height&equals;"347" &sol;><&sol;p>&NewLine;<p>Let&&num;8217&semi;s see one more use case in which we are going to use the third type of guideline &lpar;i&period;e&period; <span class&equals;"s2"><strong><span style&equals;"color&colon; &num;0000ff&semi;">layout&lowbar;constraintGuide&lowbar;percent<&sol;span><&sol;strong><&sol;span>&rpar; which positions the guideline at a certain percentage of parent&&num;8217&semi;s width&period;<&sol;p>&NewLine;<p>Using multiple guidelines of such type to make the dimension of a view to be of a certain percentage of the parent&&num;8217&semi;s width or height&period;<&sol;p>&NewLine;<p>For now&comma; we are making the width of the ImageView to be of a certain percentage of the parent&&num;8217&semi;s width&period;<&sol;p>&NewLine;<p>In the above figure&comma; the left guideline is at 25&percnt; of the parent&&num;8217&semi;s width and the right guideline is at 75&percnt; of the parent&&num;8217&semi;s width&period; Now irrespective of the screen size the width of the ImageView will always be 50&percnt; of the screen width&period;<&sol;p>&NewLine;<h4><&sol;h4>&NewLine;<p><img class&equals;"size-full wp-image-1391 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;guideline&lowbar;percentage&period;gif" alt&equals;"guideline&lowbar;percentage" width&equals;"474" height&equals;"566" &sol;><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Barrier<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>A <span style&equals;"color&colon; &num;008000&semi;"><strong>barrier<&sol;strong><&sol;span> is a view that takes references of multiple widgets as input and creates a virtual guideline based on the most extreme widget on the specified side&period;<&sol;p>&NewLine;<p>Similar to a guideline&comma; a barrier is an invisible line that you can constrain views to&period; Except a barrier does not define its own position&semi; instead&comma; the barrier position moves based on the position of views contained within it&period;<&sol;p>&NewLine;<p>This is useful when you want to constrain a view to a set of views rather than to one specific view&period;<&sol;p>&NewLine;<p>Let&&num;8217&semi;s take a use case and understand the barrier&period;<&sol;p>&NewLine;<p>Here we have three TextViews what we want is we want TextView with long text to be on the right of <span style&equals;"color&colon; &num;008000&semi;"><strong>Constraint<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>Linear<&sol;strong><&sol;span> TextViews&period; So we are going to constrain the left anchor point of longer TextView with the right anchor point of <span style&equals;"color&colon; &num;008000&semi;"><strong>Constraint<&sol;strong><&sol;span> TextView&period;<&sol;p>&NewLine;<p><img class&equals;" size-full wp-image-1394 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-30-15&period;32&period;20-2659785624-1575296885415&period;png" alt&equals;"Screenshot 2019-11-30 15&period;32&period;20" width&equals;"430" height&equals;"257" &sol;><&sol;p>&NewLine;<p>In case if we change the text <span style&equals;"color&colon; &num;008000&semi;"><strong>Linear<&sol;strong><&sol;span> to <strong><span style&equals;"color&colon; &num;008000&semi;">Linearlayoutt<&sol;span><&sol;strong> you can see the TextView with longer text run over the TextViews on the left of it&period; To solve this problem we can make use of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>barrier<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><img class&equals;" size-full wp-image-1395 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;Screenshot-2019-11-30-15&period;37&period;15-4061154534-1575297117902&period;png" alt&equals;"Screenshot 2019-11-30 15&period;37&period;15" width&equals;"428" height&equals;"263" &sol;><&sol;p>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Creating Barrier<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>To create a barrier&comma; follow these steps&colon;<&sol;p>&NewLine;<ol>&NewLine;<li>Click <span style&equals;"color&colon; &num;008000&semi;"><strong>Guidelines<&sol;strong><&sol;span> <img class&equals;"inline-icon" src&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;studio&sol;images&sol;buttons&sol;layout-editor-guidelines&period;png" alt&equals;"" width&equals;"21" height&equals;"21" &sol;> in the toolbar&comma; and then click <span style&equals;"color&colon; &num;008000&semi;"><strong>Add Vertical Barrier<&sol;strong> <span style&equals;"color&colon; &num;000000&semi;">or<&sol;span> <strong>Add Horizontal Barrier<&sol;strong>&period;<&sol;span><&sol;li>&NewLine;<li>In the <span style&equals;"color&colon; &num;008000&semi;"><b>Component Tree<&sol;b><&sol;span> window&comma; select the views you want inside the barrier and drag them into the barrier component&period;<&sol;li>&NewLine;<li>Select the barrier from the <span style&equals;"color&colon; &num;008000&semi;"><b>Component Tree<&sol;b><&sol;span>&comma; open the <span style&equals;"color&colon; &num;008000&semi;"><b>Attributes<&sol;b><&sol;span> <img class&equals;"inline-icon" src&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;studio&sol;images&sol;buttons&sol;window-properties&period;png" alt&equals;"" width&equals;"24" height&equals;"24" &sol;> window&comma; and then set the <span style&equals;"color&colon; &num;008000&semi;"><b>barrierDirection<&sol;b><&sol;span>&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>To solve this issue here we make use of <span style&equals;"color&colon; &num;008000&semi;"><strong>Vertical<&sol;strong><&sol;span> barrier&period;<&sol;p>&NewLine;<p>Barrier basically takes reference id of views as input to create a barrier for them&period; So we are going to add references of <span style&equals;"color&colon; &num;008000&semi;"><strong>Constraint<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>LinearLayoutt<&sol;strong><&sol;span> TextViews&period;<&sol;p>&NewLine;<p>Select <span style&equals;"color&colon; &num;008000&semi;"><strong>barrier<&sol;strong><&sol;span> from the component tree and change barrier direction to <span style&equals;"color&colon; &num;008000&semi;"><strong>end<&sol;strong><&sol;span>&period; Now to get the expected result constrain the left anchor point of longer TextView with the barrier&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1396" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;11&sol;barrier&period;gif" alt&equals;"barrier" width&equals;"722" height&equals;"372" &sol;><&sol;p>&NewLine;<p>Above you can see the barrier adjusted its position according to the view with the larger size&period; This is how the barrier works&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Group<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Group<&sol;strong><&sol;span> is a helper view that controls the visibility of a set of referenced widgets&period; It is a convenient way to hide or show a set of widgets&period;<&sol;p>&NewLine;<p>Using Group we don&&num;8217&semi;t have to set the visibility of each widget individually and same as <strong><span style&equals;"color&colon; &num;008000&semi;">barrier<&sol;span><&sol;strong> it takes references of multiple widgets&period;<&sol;p>&NewLine;<p>Group is useful in the case where we want to change the visibility of a set of views like in case of <strong><span style&equals;"color&colon; &num;008000&semi;">error occurred<&sol;span><&sol;strong>&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>loading<&sol;strong><&sol;span> where we don&&num;8217&semi;t want to show a set of widgets&period;<&sol;p>&NewLine;<p>In the below-animated figure&comma; you can see after setting the visibility of the group to <strong><span style&equals;"color&colon; &num;008000&semi;">gone<&sol;span><&sol;strong> it is no more visible on the screen&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1399" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;12&sol;group&period;gif" alt&equals;"group" width&equals;"658" height&equals;"408" &sol;><&sol;p>&NewLine;<p>In your xml file you can see the references of the views has been added to the Group like this&colon;<&sol;p>&NewLine;<pre>&lt&semi;androidx&period;constraintlayout&period;widget&period;Group&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;group2"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;visibility&equals;"invisible"&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>app&colon;constraint&lowbar;referenced&lowbar;ids&equals;"button&comma;imageView&comma;textView"<&sol;strong><&sol;span> &sol;&gt&semi;<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<p>I hope this article will help you in understanding various types of constraints that you can use to build a complex layout with <strong><span style&equals;"color&colon; &num;008000&semi;">Constraintlayout<&sol;span><&sol;strong>&period;&NewLine;

Exit mobile version