<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ramsingh Manek</title>
    <description>The latest articles on DEV Community by Ramsingh Manek (@bestremoteteam).</description>
    <link>https://dev.to/bestremoteteam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1026482%2F09639319-7521-4f97-a9b0-4a8848718e2f.jpg</url>
      <title>DEV Community: Ramsingh Manek</title>
      <link>https://dev.to/bestremoteteam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bestremoteteam"/>
    <language>en</language>
    <item>
      <title>Complete Guide to Layouts in Compose - Android Jetpack Compose</title>
      <dc:creator>Ramsingh Manek</dc:creator>
      <pubDate>Tue, 04 Apr 2023 09:41:31 +0000</pubDate>
      <link>https://dev.to/bestremoteteam/complete-guide-to-layouts-in-compose-android-jetpack-compose-223n</link>
      <guid>https://dev.to/bestremoteteam/complete-guide-to-layouts-in-compose-android-jetpack-compose-223n</guid>
      <description>&lt;p&gt;The Layouts composable are a very essential and important component for creating UI. Jetpack compose provides predefined layouts like Column, Row, Box, and ConstraintLayout.&lt;/p&gt;

&lt;p&gt;Common Attributes for the layout composable:&lt;/p&gt;

&lt;h2&gt;
  
  
  Showing widgets vertically using Column:
&lt;/h2&gt;

&lt;p&gt;In Composable there is a Column as a LinearLayout with vertical orientation. It simply layouts all child widgets as vertical columns. All the children in the Column layout will be arranged vertically at the top position and in horizontal alignment at the start position, by default. Update the above function as below:&lt;/p&gt;

&lt;p&gt;@Preview&lt;br&gt;
@Composable&lt;br&gt;
fun ColumnExample() {&lt;/p&gt;

&lt;p&gt;Column(&lt;br&gt;
       modifier = Modifier.fillMaxSize()&lt;br&gt;
   ) {&lt;br&gt;
       Image(&lt;br&gt;
           bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small),&lt;br&gt;
           contentDescription = null&lt;br&gt;
       )&lt;br&gt;
       Text(text = "Espresso", fontSize = 25.sp)&lt;br&gt;
   }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;In the above example, we have not used any alignment-related property. So let's see how the output would be if we set alignment to image and text compostables.&lt;/p&gt;

&lt;h3&gt;
  
  
  Weight
&lt;/h3&gt;

&lt;p&gt;We can divide the components into equal portions using the weight in the modifier in the Column layout using the weight(weight value) modifier. Let’s see a basic example below:&lt;/p&gt;

&lt;p&gt;@Preview&lt;br&gt;
@Composable&lt;br&gt;
fun ColumnExampleWithWeight() {&lt;br&gt;
   Column(&lt;br&gt;
       horizontalAlignment = Alignment.CenterHorizontally,&lt;br&gt;
       modifier = Modifier&lt;br&gt;
           .fillMaxWidth()&lt;br&gt;
   ) {&lt;br&gt;
       Image(&lt;br&gt;
           bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small),&lt;br&gt;
           contentDescription = null,&lt;br&gt;
           Modifier.weight(2f)&lt;br&gt;
       )&lt;br&gt;
       Text(&lt;br&gt;
           text = "Espresso",&lt;br&gt;
           fontSize = 25.sp,&lt;br&gt;
           modifier = Modifier&lt;br&gt;
               .weight(2f)&lt;br&gt;
               .align(Alignment.CenterHorizontally)&lt;br&gt;
       )&lt;br&gt;
   }&lt;br&gt;
}&lt;/p&gt;

&lt;h3&gt;
  
  
  Layout Alignment
&lt;/h3&gt;

&lt;p&gt;By default, all children in the Column are aligned in the top-left corner position. We can align the children in the Column layout using the following alignments&lt;/p&gt;

&lt;p&gt;With the Column layout, we have constant Alignment.CenterHorizontal interface are:&lt;br&gt;&lt;br&gt;
1. Start &lt;br&gt;&lt;br&gt;
2.CenterHorizontally&lt;br&gt;&lt;br&gt;
3. End&lt;br&gt;&lt;br&gt;
The above parameter is used to align the components horizontally along the horizontal axis.&lt;/p&gt;

&lt;p&gt;Column(&lt;br&gt;
   modifier = Modifier&lt;br&gt;
       .fillMaxSize()&lt;br&gt;
       .background(Color.Black),&lt;br&gt;
   horizontalAlignment = Alignment.CenterHorizontally,&lt;br&gt;
) {&lt;br&gt;
   Image(&lt;br&gt;
       bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small),&lt;br&gt;
       contentDescription = null&lt;br&gt;
   )&lt;br&gt;
   Text(text = "Espresso", fontSize = 25.sp, color = Color.White)&lt;br&gt;
}&lt;/p&gt;

&lt;h3&gt;
  
  
  Layout Arrangement
&lt;/h3&gt;

&lt;p&gt;The arrangement can be used to control the positions of the child along the same axis as the parent container like vertically for Column. Arrangements values can be set on the Column layout using vertical arrangement parameters. &lt;/p&gt;

&lt;p&gt;The column composable has a vertical arrangement parameter which accepts the values as follows&lt;br&gt;&lt;br&gt;
1. Arrangement.Top - Arrange the content at the top of the content area vertically&lt;br&gt;&lt;br&gt;
2. Arrangement. Center - Arrange the content at the center vertically in the content area&lt;br&gt;&lt;br&gt;
3. Arrangement.Bottom - Arrange the content at the bottom of the content area vertically.&lt;/p&gt;

&lt;p&gt;Column(&lt;br&gt;
   modifier = Modifier&lt;br&gt;
       .fillMaxSize()&lt;br&gt;
       .background(Color.Black),&lt;br&gt;
horizontalAlignment = Alignment.CenterHorizontally,&lt;br&gt;
verticalArrangement = Arrangement.Center&lt;br&gt;
) {&lt;br&gt;
   Image(&lt;br&gt;
       bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small),&lt;br&gt;
       contentDescription = null&lt;br&gt;
   )&lt;br&gt;
   Text(text = "Espresso", fontSize = 25.sp, color = Color.White)&lt;br&gt;
}&lt;/p&gt;

&lt;h2&gt;
  
  
  Showing widgets horizontally using Row:
&lt;/h2&gt;

&lt;p&gt;In Composable there is a Row as a LinearLayout with horizontal orientation. It simply layouts all child widgets as horizontal rows. All the children in the Row layout will be arranged vertically at the top position and in horizontal alignment at the start position, by default. Update the above function as below:&lt;/p&gt;

&lt;p&gt;Row(&lt;br&gt;
   modifier = Modifier&lt;br&gt;
       .fillMaxSize()&lt;br&gt;
       .background(Color.Black),&lt;br&gt;
) {&lt;br&gt;
   Image(&lt;br&gt;
       bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small),&lt;br&gt;
       contentDescription = null,&lt;br&gt;
       modifier = Modifier.size(100.dp)&lt;br&gt;
   )&lt;br&gt;
   Text(text = "Espresso", fontSize = 25.sp, color = Color.White)&lt;br&gt;
}&lt;/p&gt;

&lt;h3&gt;
  
  
  Weight
&lt;/h3&gt;

&lt;p&gt;We can add the weight in the modifier in the Row layout using the weight(weight value) modifier. Let’s see a basic example below:&lt;/p&gt;

&lt;p&gt;@Preview&lt;br&gt;
@Composable&lt;br&gt;
fun RowExampleWithWeight() {&lt;/p&gt;

&lt;p&gt;Row(&lt;br&gt;
       modifier = Modifier&lt;br&gt;
           .fillMaxSize()&lt;br&gt;
           .background(Color.Black),&lt;br&gt;
   ) {&lt;br&gt;
       Image(&lt;br&gt;
           bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small),&lt;br&gt;
           contentDescription = null,&lt;br&gt;
           modifier = Modifier.weight(1f)&lt;br&gt;
       )&lt;br&gt;
       Text(&lt;br&gt;
           text = "Espresso",&lt;br&gt;
           fontSize = 25.sp,&lt;br&gt;
           color = Color.White,&lt;br&gt;
           modifier = Modifier.weight(1f)&lt;br&gt;
       )&lt;br&gt;
   }&lt;br&gt;
}&lt;/p&gt;

&lt;h3&gt;
  
  
  Layout Alignment
&lt;/h3&gt;

&lt;p&gt;By default, all children in Row are aligned in the top-left corner position. We can align the children in the Row layout using the following alignments&lt;/p&gt;

&lt;p&gt;With the Row layout, we have constant Alignment.CenterVertical interface is:&lt;br&gt;&lt;br&gt;
4. Top&lt;br&gt;&lt;br&gt;
5.CenterVertically&lt;br&gt;&lt;br&gt;
6. Bottom&lt;br&gt;&lt;br&gt;
The above parameter is used to align the components horizontally along the horizontal axis.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layout Arrangement
&lt;/h3&gt;

&lt;p&gt;The arrangement can be used to control the positions of the child along the same axis as the parent container like horizontally for Rows. Arrangements values can be set on the Row layout using horizontal arrangement parameters.&lt;/p&gt;

&lt;p&gt;The column composable has a vertical arrangement parameter that accepts the values as follows&lt;br&gt;&lt;br&gt;
1. Arrangement. Start - Arrange the content at the start of the row horizontally to the content area&lt;br&gt;&lt;br&gt;
2. Arrangement.Center - Arrange the content at the center horizontally in the content area&lt;br&gt;&lt;br&gt;
3. Arrangement.End - Arrange the content at the end of the row to the content area horizontally.&lt;/p&gt;

&lt;p&gt;Row(&lt;br&gt;
   modifier = Modifier&lt;br&gt;
       .fillMaxSize()&lt;br&gt;
       .background(Color.Black),&lt;br&gt;
   verticalAlignment =Alignment.CenterVertically&lt;br&gt;
) {&lt;br&gt;
   Image(&lt;br&gt;
       bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small),&lt;br&gt;
       contentDescription = null,&lt;br&gt;
       modifier = Modifier.weight(1f)&lt;br&gt;
   )&lt;br&gt;
   Text(&lt;br&gt;
       text = "Espresso",&lt;br&gt;
       fontSize = 25.sp,&lt;br&gt;
       color = Color.White,&lt;br&gt;
       modifier = Modifier.weight(1f)&lt;br&gt;
   )&lt;br&gt;
}&lt;/p&gt;

&lt;h3&gt;
  
  
  Layout Arrangement Spacing
&lt;/h3&gt;

&lt;p&gt;Arranging space between each child component in Rows or Columns is spaced across the content area. The spacing can be achieved by using the horizontal arrangement and vertical arrangement parameters that require one of the following values.&lt;br&gt;&lt;br&gt;
1. Arrangement.SpaceEvenly - All child components are spaced equally, including before the first component and after the last component.&lt;br&gt;&lt;br&gt;
2. Arrangement.SpaceBetween - All children are spaced equally in the parent layout. There is no space added before the first and after the last child.&lt;br&gt;&lt;br&gt;
3. Arrangement.SpaceAround - All children are spaced equally in the parent layout. It includes half spacing before the first and after the last child. &lt;/p&gt;

&lt;p&gt;Below example are used with Row composable&lt;br&gt;&lt;br&gt;
1.SpaceEvenly:&lt;/p&gt;

&lt;p&gt;@Composable&lt;br&gt;
fun TextCell(value: String) {&lt;br&gt;
   val cellModifier = Modifier&lt;br&gt;
       .padding(5.dp)&lt;br&gt;
       .size(100.dp, 100.dp)&lt;br&gt;
       .border(width = 2.dp, color = Color.Red)&lt;br&gt;
       .wrapContentHeight()&lt;/p&gt;

&lt;p&gt;Text(&lt;br&gt;
       text = value,&lt;br&gt;
       modifier = cellModifier,&lt;br&gt;
       fontSize = 20.sp,&lt;br&gt;
       fontWeight = FontWeight.Bold,&lt;br&gt;
       textAlign = TextAlign.Center&lt;br&gt;
   )&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Now, call the above function to create Text composable as follows:&lt;/p&gt;

&lt;p&gt;Row(&lt;br&gt;
   verticalAlignment = Alignment.CenterVertically,&lt;br&gt;
   horizontalArrangement = Arrangement.SpaceEvenly,&lt;br&gt;
   modifier = Modifier.fillMaxSize()&lt;br&gt;
) {&lt;br&gt;
   TextCell(value = "A")&lt;br&gt;
   TextCell(value = "B")&lt;br&gt;
   TextCell(value = "C")&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;2.SpaceBetween&lt;/p&gt;

&lt;p&gt;Row(&lt;br&gt;
   verticalAlignment = Alignment.CenterVertically,&lt;br&gt;
   horizontalArrangement = Arrangement.SpaceBetween,&lt;br&gt;
   modifier = Modifier.fillMaxSize()&lt;br&gt;
) {&lt;br&gt;
   TextCell(value = "A")&lt;br&gt;
   TextCell(value = "B")&lt;br&gt;
   TextCell(value = "C")&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;3.SpaceAround&lt;/p&gt;

&lt;p&gt;Row(&lt;br&gt;
   verticalAlignment = Alignment.CenterVertically,&lt;br&gt;
   horizontalArrangement = Arrangement.SpaceAround,&lt;br&gt;
   modifier = Modifier.fillMaxSize()&lt;br&gt;
) {&lt;br&gt;
   TextCell(value = "A")&lt;br&gt;
   TextCell(value = "B")&lt;br&gt;
   TextCell(value = "C")&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Below examples are used with Column composable. Also, we will use the same TextCell() for column example&lt;/p&gt;

&lt;p&gt;1.SpaceEvenly&lt;/p&gt;

&lt;p&gt;Column(&lt;br&gt;
   horizontalAlignment = Alignment.CenterHorizontally,&lt;br&gt;
   verticalArrangement = Arrangement.SpaceEvenly,&lt;br&gt;
   modifier = Modifier.height(600.dp)&lt;br&gt;
) {&lt;br&gt;
   TextCell(value = "A")&lt;br&gt;
   TextCell(value = "B")&lt;br&gt;
   TextCell(value = "C")&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;2.SpaceBetween&lt;/p&gt;

&lt;p&gt;Column(&lt;br&gt;
   horizontalAlignment = Alignment.CenterHorizontally,&lt;br&gt;
   verticalArrangement = Arrangement.SpaceBetween,&lt;br&gt;
   modifier = Modifier.height(600.dp)&lt;br&gt;
) {&lt;br&gt;
   TextCell(value = "A")&lt;br&gt;
   TextCell(value = "B")&lt;br&gt;
   TextCell(value = "C")&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;3.SpaceAround&lt;/p&gt;

&lt;p&gt;Column(&lt;br&gt;
   horizontalAlignment = Alignment.CenterHorizontally,&lt;br&gt;
   verticalArrangement = Arrangement.SpaceAround,&lt;br&gt;
   modifier = Modifier.height(600.dp)&lt;br&gt;
) {&lt;br&gt;
   TextCell(value = "A")&lt;br&gt;
   TextCell(value = "B")&lt;br&gt;
   TextCell(value = "C")&lt;br&gt;
}&lt;/p&gt;

&lt;h2&gt;
  
  
  Box Layout
&lt;/h2&gt;

&lt;p&gt;Boxes are the third most used composable when developing applications using jetpack compose. The box composable will stack the child on top of each other. By default, the children will be placed at the top-start position. The stacking order is the order in which children are declared in the box layout, the first child will be at the bottom of the stack and the last child will be visible on the top of the stack.&lt;/p&gt;

&lt;h3&gt;
  
  
  Aligning the child in the Box Layout
&lt;/h3&gt;

&lt;p&gt;Using the Box layout we can specify the position of the child where we want to align our child elements inside our box. There are 3 vertical options available top, center, and bottom. Horizontal options are, start, center, and end. So there are 9 possible alignments for our child's in-box layout as shown below table:&lt;/p&gt;

&lt;p&gt;Here is a simple example of a box layout&lt;/p&gt;

&lt;p&gt;Box(contentAlignment = Alignment.Center,&lt;br&gt;
   modifier = Modifier.size(400.dp, 400.dp)) {&lt;/p&gt;

&lt;p&gt;TextCell("1")&lt;br&gt;
   TextCell("2")&lt;br&gt;
   TextCell("3")&lt;br&gt;
}&lt;/p&gt;

&lt;h3&gt;
  
  
  BoxScope modifiers
&lt;/h3&gt;

&lt;p&gt;The following BoxScope modifiers are available that need to be applied to child components in the box layout.&lt;br&gt;&lt;br&gt;
1. align() - Aligns the child to a specified alignment value within the Box content layout. The set of alignment values is the same as those listed above for box content alignment.&lt;/p&gt;

&lt;p&gt;2.matchParentSize() - If this modifier is specified to the child then the size of the child will be the same as the parent box.&lt;/p&gt;

&lt;p&gt;Now, add the following function to add text cells in the box layout as follows to add text layout to all different positions.&lt;/p&gt;

&lt;p&gt;@Composable&lt;br&gt;
fun TextCellBox(value: String, modifier: Modifier) {&lt;br&gt;
   val cellModifier = Modifier&lt;br&gt;
       .wrapContentHeight()&lt;br&gt;
       .size(30.dp, 30.dp)&lt;br&gt;
       .border(width = 2.dp, color = Color.Red)&lt;/p&gt;

&lt;p&gt;Text(&lt;br&gt;
       text = value,&lt;br&gt;
       modifier = cellModifier.then(modifier),&lt;br&gt;
       fontSize = 20.sp,&lt;br&gt;
       fontWeight = FontWeight.Bold,&lt;br&gt;
       textAlign = TextAlign.Center&lt;br&gt;
   )&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Here is the box example for adding text to all positions.&lt;/p&gt;

&lt;p&gt;Box(modifier = Modifier.size(height = 150.dp, width = 200.dp).padding(15.dp)) {&lt;br&gt;
   TextCellBox(value = "A", modifier = Modifier.align(Alignment.TopStart))&lt;br&gt;
   TextCellBox(value = "B", modifier = Modifier.align(Alignment.TopCenter))&lt;br&gt;
   TextCellBox(value = "C", modifier = Modifier.align(Alignment.TopEnd))&lt;br&gt;
   TextCellBox(value = "D", modifier = Modifier.align(Alignment.CenterStart))&lt;br&gt;
   TextCellBox(value = "E", modifier = Modifier.align(Alignment.Center))&lt;br&gt;
   TextCellBox(value = "F", modifier = Modifier.align(Alignment.CenterEnd))&lt;br&gt;
   TextCellBox(value = "G", modifier = Modifier.align(Alignment.BottomStart))&lt;br&gt;
   TextCellBox(value = "H", modifier = Modifier.align(Alignment.BottomCenter))&lt;br&gt;
   TextCellBox(value = "I", modifier = Modifier.align(Alignment.BottomEnd))&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Let's now check how the matchParentSize() modifier will work by a simple example.&lt;/p&gt;

&lt;p&gt;Box(&lt;br&gt;
   Modifier&lt;br&gt;
       .size(200.dp)&lt;br&gt;
       .background(Color.Blue)&lt;br&gt;
)&lt;br&gt;
{&lt;/p&gt;

&lt;p&gt;Text(&lt;br&gt;
       text = "CENTER",&lt;br&gt;
       modifier = Modifier&lt;br&gt;
           .padding(10.dp)&lt;br&gt;
           .background(Color.Red)&lt;br&gt;
           .matchParentSize()&lt;br&gt;
           .wrapContentHeight(),&lt;br&gt;
       fontSize = 20.sp,&lt;br&gt;
       fontWeight = FontWeight.Bold,&lt;br&gt;
       textAlign = TextAlign.Center&lt;br&gt;
   )&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;h3&gt;
  
  
  Using the clip() modifier
&lt;/h3&gt;

&lt;p&gt;The clip() modifier is used to render the specified shape to box composable layout. To define the shape of the composable, the clip() modifier is called with values such as RectangleShape, CircleShape, RoundedCornerShape, or CutCornerShape. &lt;/p&gt;

&lt;p&gt;The following example will clip the box composable and draw the circle with blue color. When rendered it will look like the box appears in the below image.&lt;/p&gt;

&lt;p&gt;Box(&lt;br&gt;
   Modifier&lt;br&gt;
       .size(300.dp)&lt;br&gt;
       .padding(50.dp)&lt;br&gt;
       .clip(CircleShape)&lt;br&gt;
       .background(Color.Blue)&lt;br&gt;
)&lt;/p&gt;

&lt;p&gt;To draw different shapes like rounded corners just change the value in the clip() modifier as RoundedCorderShape, and pass the radius for each direction to it to draw the required corners. If only a single value is passed as a radius then it will be applied the same to all corners. Let's see a simple example with a rounded corner box and a simple “Hello” text inside it.&lt;/p&gt;

&lt;p&gt;Box(&lt;br&gt;
   Modifier&lt;br&gt;
       .clip(RoundedCornerShape(15.dp))&lt;br&gt;
       .size(300.dp)&lt;br&gt;
       .padding(50.dp)&lt;br&gt;
       .background(Color.Blue)&lt;br&gt;
)&lt;br&gt;
{&lt;/p&gt;

&lt;p&gt;Text(&lt;br&gt;
       text = "Hello!",&lt;br&gt;
       modifier = Modifier&lt;br&gt;
           .padding(20.dp)&lt;br&gt;
           .clip(RoundedCornerShape(15.dp))&lt;br&gt;
           .background(Color.Red)&lt;br&gt;
           .matchParentSize()&lt;br&gt;
           .wrapContentHeight(),&lt;br&gt;
       fontSize = 20.sp,&lt;br&gt;
       fontWeight = FontWeight.Bold,&lt;br&gt;
       textAlign = TextAlign.Center&lt;br&gt;
   )&lt;br&gt;
}&lt;/p&gt;

&lt;h2&gt;
  
  
  Constraint Layout
&lt;/h2&gt;

&lt;p&gt;The ConstraintLayout positions the children according to the constraint settled between them. It is similar to “ConstraintLayout” which we use to add views in XML for Android Development.&lt;/p&gt;

&lt;h3&gt;
  
  
  Generating References
&lt;/h3&gt;

&lt;p&gt;By default, the children in ConstraintLayout will be positioned on the top-left-hand corner of the content area with the assumption of the app running on left to right and top-to-bottom locale. Before assigning the constraints the compostables must assign a reference. There are two steps in the process generating the references and assigning them to the composable before applying the constraints. &lt;/p&gt;

&lt;p&gt;createRef() function is used to create a single reference and result assigned to constant, for example&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        Val text1 = createRef()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Alternatively, if you want to create multiple references in a single step by calling same createRefs() function as shown below:&lt;/p&gt;

&lt;p&gt;Val (text1, text2, text3) = createRefs()&lt;/p&gt;

&lt;h3&gt;
  
  
  Assigning references to a composable 
&lt;/h3&gt;

&lt;p&gt;After creating the reference, it can be applied to an individual composable using the constraints () modifier function. For example, assign text1 reference to Text composable. As we can see in the given example, the constraints () modifier function has a trailing lambda in which we can specify the constraint for the composable.&lt;/p&gt;

&lt;p&gt;ConstraintLayout{&lt;br&gt;
    Val img = createRef()&lt;br&gt;
Image(&lt;br&gt;
    Bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small), contentDescription = null, modifier = Modifier.constraintAs(img){&lt;br&gt;
// add constraints here&lt;br&gt;
})&lt;br&gt;&lt;br&gt;
}&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Constraint
&lt;/h3&gt;

&lt;p&gt;The most common constraint of the composable is between one side of the composable and another side of either another composable or parent ConstraintLayout itself. The constraints are added using the constraints () modifier function and declared inside the training lambda block via the call to link () function.&lt;/p&gt;

&lt;p&gt;Let’s see how we can implement constraint layout by using the link () function as shown below by updating the above sample code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compose example 1.0
&lt;/h3&gt;

&lt;p&gt;ConstraintLayout{&lt;br&gt;
    Val img = createRef()&lt;br&gt;
Image(&lt;br&gt;
    Bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small), contentDescription = null, modifier = Modifier.constraintAs(img){&lt;br&gt;
    top.linkTo(parent.top, margin = 16.dp)&lt;br&gt;
    start.linkTo(parent.start, margin = 16.dp) &lt;br&gt;
})&lt;br&gt;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;In the above example, we have set the constraint of the image view to the left - the top position of the parent ConstraintLayout instance, both with 16. dp margin.&lt;/p&gt;

&lt;p&gt;In addition to the linkTo() function there are other functions to apply constraints, a component can be centered horizontally or vertically relative to the parent itself or any other component using centerHorizontallyTo() and centerVerticallyTo() respectively. For example,&lt;/p&gt;

&lt;p&gt;Image(&lt;br&gt;
    Bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small), contentDescription = null, modifier = Modifier.constraintAs(img){&lt;br&gt;
    centerVerticallyTo(parent)&lt;br&gt;
    centerHorizontallyTo(parent)&lt;br&gt;
})&lt;/p&gt;

&lt;p&gt;In the above example, the image will be positioned vertically from center to parent on the vertical axis, and horizontally from center to parent on the horizontal axis of the ConstraintLayout.&lt;/p&gt;

&lt;p&gt;centerAround() function can be used to position the component in the center vertically or horizontally relative to the side of another component.&lt;/p&gt;

&lt;p&gt;Now, after learning to add constraints let’s create a simple view with an image and some text as below.&lt;/p&gt;

&lt;p&gt;ConstraintLayout(&lt;br&gt;
       modifier = Modifier&lt;br&gt;
           .fillMaxSize()&lt;br&gt;
           .background(Color.Black)&lt;br&gt;
   ) {&lt;br&gt;
       val img = createRef()&lt;br&gt;
       val txtTitle = createRef()&lt;br&gt;
       val txtDescription = createRef()&lt;br&gt;
       val txtTagline = createRef()&lt;br&gt;
       Image(&lt;br&gt;
           bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small),&lt;br&gt;
           contentDescription = null,&lt;br&gt;
           modifier = Modifier&lt;br&gt;
               .constrainAs(img) {&lt;br&gt;
                   top.linkTo(parent.top, margin = 16.dp)&lt;br&gt;
                   start.linkTo(parent.start, margin = 16.dp)&lt;br&gt;
               }&lt;br&gt;
               .background(Color.Blue)&lt;br&gt;
               .clip(RoundedCornerShape(10.dp))&lt;br&gt;
               .size(80.dp)&lt;br&gt;
               .wrapContentHeight()&lt;br&gt;
       )&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Text("Espresso", modifier = Modifier.constrainAs(txtTitle) {
       top.linkTo(parent.top)
       linkTo(start = img.end, end = parent.end, startMargin = 10.dp, endMargin = 16.dp)
       width = Dimension.fillToConstraints
   }, fontSize = 24.sp, color = Color.White)

   Text(
       text = "Espresso is coffee of Italian origin, brewed by forcing a small amount of nearly boiling water under pressure (expressing) through finely-ground coffee beans.",
       modifier = Modifier.constrainAs(txtDescription) {
           top.linkTo(txtTitle.bottom, margin = 5.dp)
           linkTo(txtTitle.start, txtTitle.end)
           width = Dimension.fillToConstraints
       }, color = Color.White
   )

   Text(
       text = "This is example of ConstraintLayout",
       modifier = Modifier.constrainAs(txtTagline) {
           linkTo(parent.start, parent.end)
           top.linkTo(txtDescription.bottom, margin = 10.dp)
       }, color = Color.White
   )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;h3&gt;
  
  
  Opposing Constraints
&lt;/h3&gt;

&lt;p&gt;Till now we have seen how to set constraints to components to a fixed position within the parent with margins. The opposing constraints are the constraints created with both sides of the component along the same axis horizontally or vertically are constrained. For example, applying the opposing constraint on the button on the horizontal axis.&lt;/p&gt;

&lt;p&gt;val btnOpposed = createRef()&lt;br&gt;
Button(onClick = { }, modifier = Modifier.constrainAs(btnOpposed) {&lt;br&gt;
   top.linkTo(txtTagline.bottom, margin = 20.dp)&lt;br&gt;
   start.linkTo(parent.start)&lt;br&gt;
   end.linkTo(parent.end)&lt;br&gt;
}) {&lt;br&gt;
   Text("Centered Button")&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;As you can see in the above image the opposing constraint will position the component in the horizontal center position in the constraint layout. The link () function can accept the parameters to concise the opposing constraint which can be declared as the following example, by updating the above-given code.&lt;/p&gt;

&lt;p&gt;val btnOpposed = createRef()&lt;br&gt;
Button(onClick = { }, modifier = Modifier.constrainAs(btnOpposed) {&lt;br&gt;
   top.linkTo(txtTagline.bottom, margin = 20.dp)&lt;br&gt;
   linkTo(parent.start, parent.end)&lt;br&gt;
}) {&lt;br&gt;
   Text("View Detail")&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;The link to the () function has two arguments in the lambda block, which will position the button in the center horizontally with a single line of code. If you require to position the component center vertically or center horizontally, within the parent, the same result can be achieved using the following line of code.&lt;/p&gt;

&lt;p&gt;centerVerticallyTo(parent)&lt;br&gt;
centerHorizontallyTo(parent)&lt;/p&gt;

&lt;h3&gt;
  
  
  Constraint Bais
&lt;/h3&gt;

&lt;p&gt;To arrange a component to center position on a particular axis opposing constraint is used. When the bias is applied to a constrained component, it will be moved relative to the available space. Let's understand the basis constraint with a simple example. &lt;/p&gt;

&lt;p&gt;Add the following code to the previous compose example 1.0 and update the button code block as below.&lt;/p&gt;

&lt;p&gt;val btnOpposed = createRef()&lt;br&gt;
val (imgLlike, imgShare) = createRefs()&lt;/p&gt;

&lt;p&gt;Image(&lt;br&gt;
   Icons.Filled.Favorite,&lt;br&gt;
   contentDescription = null,&lt;br&gt;
   modifier = Modifier.constrainAs(imgLlike) {&lt;br&gt;
       linkTo(btnOpposed.top, btnOpposed.bottom)&lt;br&gt;
       linkTo(parent.start, parent.end, bias = 0.05f)&lt;br&gt;
   },&lt;br&gt;
   colorFilter = ColorFilter.tint(Color.White)&lt;br&gt;
)&lt;/p&gt;

&lt;p&gt;Image(&lt;br&gt;
   Icons.Filled.Share,&lt;br&gt;
   contentDescription = null,&lt;br&gt;
   modifier = Modifier.constrainAs(imgShare) {&lt;br&gt;
       linkTo(btnOpposed.top, btnOpposed.bottom)&lt;br&gt;
       linkTo(parent.start, parent.end, bias = 0.15f)&lt;br&gt;
   }, colorFilter = ColorFilter.tint(Color.White)&lt;br&gt;
)&lt;/p&gt;

&lt;p&gt;Button(onClick = { }, modifier = Modifier.constrainAs(btnOpposed) {&lt;br&gt;
   top.linkTo(txtTagline.bottom, margin = 20.dp)&lt;br&gt;
   linkTo(parent.start, parent.end, bias = 0.9f)&lt;br&gt;
}) {&lt;br&gt;
   Text("View Detail")&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;In this example button “View Detail” will be positioned at 90% of the width of the parent. Same way icons of share and favorite have been constrained using bias and positioned at 5% and 15% of the parent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Constraint Margin
&lt;/h3&gt;

&lt;p&gt;We can set the margin to a composable to fix the gap according to our needs, between a component and another element like a composable barrier, guideline, or the side of the parent ConstraintLayout.&lt;/p&gt;

&lt;p&gt;Please consider the above example for margin. The horizontal bias constraint setting will control the position of the component on the right-hand side of the layout.&lt;/p&gt;

&lt;p&gt;After adding a margin constraint to a component a fixed gap appears and a component can not be moved even when adjusting the bias.  In the code given below margin of 30 dp is added and the right-hand constraint into the button is composable and can not be moved even when setting bias to 100%.&lt;/p&gt;

&lt;p&gt;val btnClose = createRef()&lt;br&gt;
Button(onClick = {}, modifier = Modifier.constrainAs(btnClose) {&lt;br&gt;
   bottom.linkTo(parent.bottom, margin = 30.dp)&lt;br&gt;
   linkTo(parent.start, parent.end, bias = 1.0f, endMargin = 30.dp)&lt;br&gt;
}) {&lt;br&gt;
   Text(text = "Close")&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;This margin on the right-hand side of the button will be preserved even if screen orientation changes or any other component or view are added to the right side of the button. Even if we don’t specify the bias setting, the margin will be applied and will not affect the positioning of the component.&lt;/p&gt;

&lt;h3&gt;
  
  
  Working with Chains in ConstraintLayout
&lt;/h3&gt;

&lt;p&gt;When we need to arrange more than two components on the same axis horizontally or vertically with equal spacing around them we can use Chain constraint in ConstraintLayout. A chain constraint for two or more components can be created by using one of the functions either createHorizontalChain() or createVerticalChain().&lt;br&gt;&lt;br&gt;
Both these functions require passing parameters of the component references created using createRef() or createRefs(). Let's understand chain constraint by a simple example, arranging the 3 buttons horizontally with equal spacing around them.&lt;/p&gt;

&lt;p&gt;val (obtnFood, obtnFashion, obtnTravel) = createRefs()&lt;br&gt;
createHorizontalChain(obtnFood, obtnFashion, obtnTravel)&lt;/p&gt;

&lt;p&gt;OutlinedButton(&lt;br&gt;
   onClick = { },&lt;br&gt;
   modifier = Modifier&lt;br&gt;
       .constrainAs(obtnFood) {&lt;br&gt;
           bottom.linkTo(btnClose.top, margin = 20.dp)&lt;br&gt;
           centerHorizontallyTo(parent)&lt;br&gt;
       }) {&lt;br&gt;
   Text(text = "Food")&lt;br&gt;
}&lt;br&gt;
OutlinedButton(&lt;br&gt;
   onClick = { },&lt;br&gt;
   modifier = Modifier&lt;br&gt;
       .constrainAs(obtnFashion) {&lt;br&gt;
           bottom.linkTo(btnClose.top, margin = 20.dp)&lt;br&gt;
           centerHorizontallyTo(parent)&lt;br&gt;
       }) {&lt;br&gt;
   Text(text = "Fashion")&lt;br&gt;
}&lt;br&gt;
OutlinedButton(&lt;br&gt;
   onClick = { },&lt;br&gt;
   modifier = Modifier&lt;br&gt;
       .constrainAs(obtnTravel) {&lt;br&gt;
           bottom.linkTo(btnClose.top, margin = 20.dp)&lt;br&gt;
           centerHorizontallyTo(parent)&lt;br&gt;
       }) {&lt;br&gt;
   Text(text = "Travel")&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;A chain constraint can also be arranged using Packed, Spread, or SpreadInside styles. &lt;br&gt;&lt;br&gt;
1. Packed: A style where all the components are grouped together and placed in the center of the available space.&lt;br&gt;&lt;br&gt;
2. Spread: A style where all components are evenly distributed&lt;br&gt;&lt;br&gt;
3.SpreadInside: A style where all components are evenly distributed but the first and last components are fixed to each end.&lt;/p&gt;

&lt;p&gt;To change the style, you need to add a style parameter to createHorizontalChain() or createVerticallyChain(). Changing the style from the default Spread chain to SpreadInside is as follows.&lt;/p&gt;

&lt;p&gt;createHorizontalChain(&lt;br&gt;
   obtnFood,&lt;br&gt;
   obtnFashion,&lt;br&gt;
   obtnTravel,&lt;br&gt;
   chainStyle = ChainStyle.SpreadInside&lt;br&gt;
)&lt;/p&gt;

&lt;p&gt;After changing the chain style to SpreadInside the following preview will be generated in the preview pane.&lt;/p&gt;

&lt;h3&gt;
  
  
  Working with Guidelines 
&lt;/h3&gt;

&lt;p&gt;ConstraintLayout provides horizontal or vertical guidelines. Guidelines are invisible lines that are not visible to users. Guidelines are helpful to the developer to constrain views and design layouts easily. Guidelines are useful when a number of components need to align to a specific axis line like horizontally or vertically. A guideline can have the value as a percentage of either the height or width of the parent. &lt;/p&gt;

&lt;p&gt;The following function call will create a vertical guideline to the starting edge of the parent. &lt;/p&gt;

&lt;p&gt;createGuidelineFromStart(fraction = 0.25f)&lt;/p&gt;

&lt;p&gt;The following function call will create a horizontal guideline to the bottom edge of the parent and the guideline will be positioned above the 60dp from the bottom edge.&lt;/p&gt;

&lt;p&gt;createGuidelineFromBottom(offset = 60.dp)&lt;/p&gt;

&lt;p&gt;Now, let's create a simple example using guidelines at 40% of the parent and show the banner image at the top above the guideline, and below the image we can show some description or other content.&lt;br&gt;&lt;br&gt;
Below the banner image, we have set the like and share images. And at the bottom, there is a button “View Detail”. The like and share images are again set with a vertical guideline, on the first half we have set the Like image, and on the other half we have set the Share image.&lt;/p&gt;

&lt;p&gt;ConstraintLayout(&lt;br&gt;
   modifier = Modifier&lt;br&gt;
       .fillMaxSize()&lt;br&gt;
) {&lt;/p&gt;

&lt;p&gt;val (img, imgLlike, imgShare, btnViewDetail) = createRefs()&lt;br&gt;
   val guideLineCoverImage = createGuidelineFromTop(0.4f)&lt;br&gt;
   val guideLineMiddleVerticalLine = createGuidelineFromStart(0.5f)&lt;/p&gt;

&lt;p&gt;Image(&lt;br&gt;
       bitmap = ImageBitmap.imageResource(id = R.drawable.espresso_small),&lt;br&gt;
       contentDescription = null,&lt;br&gt;
       modifier = Modifier&lt;br&gt;
           .constrainAs(img) {&lt;br&gt;
               top.linkTo(parent.top)&lt;br&gt;
               bottom.linkTo(guideLineCoverImage)&lt;br&gt;
               height = Dimension.fillToConstraints&lt;br&gt;
           }&lt;br&gt;
           .background(Color.Blue)&lt;br&gt;
           .wrapContentWidth()&lt;br&gt;
           .fillMaxHeight()&lt;br&gt;
   )&lt;/p&gt;

&lt;p&gt;Image(&lt;br&gt;
       Icons.Filled.Favorite,&lt;br&gt;
       contentDescription = null,&lt;br&gt;
       modifier = Modifier&lt;br&gt;
           .constrainAs(imgLlike) {&lt;br&gt;
               top.linkTo(img.bottom, 10.dp)&lt;br&gt;
               linkTo(parent.start, guideLineMiddleVerticalLine)&lt;br&gt;
           }&lt;br&gt;
           .size(50.dp),&lt;br&gt;
       colorFilter = ColorFilter.tint(Color.Gray)&lt;br&gt;
   )&lt;/p&gt;

&lt;p&gt;Image(&lt;br&gt;
       Icons.Filled.Share,&lt;br&gt;
       contentDescription = null,&lt;br&gt;
       modifier = Modifier&lt;br&gt;
           .constrainAs(imgShare) {&lt;br&gt;
               top.linkTo(img.bottom, 10.dp)&lt;br&gt;
               linkTo(guideLineMiddleVerticalLine, parent.end)&lt;br&gt;
           }&lt;br&gt;
           .size(50.dp),&lt;br&gt;
       colorFilter = ColorFilter.tint(Color.Gray)&lt;br&gt;
   )&lt;/p&gt;

&lt;p&gt;Button(onClick = { }, modifier = Modifier.constrainAs(btnViewDetail) {&lt;br&gt;
       linkTo(parent.start, parent.end)&lt;br&gt;
       bottom.linkTo(parent.bottom)&lt;br&gt;
       width = Dimension.fillToConstraints&lt;br&gt;
   }) {&lt;br&gt;
       Text("View Detail")&lt;br&gt;
   }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Here is the preview of the above code. In the image there you can see the vertical dotted line between the Like and Share images which is a guideline but when we run it is not visible to the user. &lt;/p&gt;

&lt;p&gt;Also, let's take another simple example for showing buttons using guidelines.&lt;/p&gt;

&lt;p&gt;ConstraintLayout(&lt;br&gt;
       modifier = Modifier&lt;br&gt;
           .wrapContentSize()&lt;br&gt;
           .padding(30.dp)&lt;br&gt;
   ) {&lt;br&gt;
       val (button1, button2, button3) = createRefs()&lt;br&gt;
       val verticalGuideline = createGuidelineFromStart(0.6f)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Button(onClick = { }, modifier = Modifier.constrainAs(button1) {
       top.linkTo(parent.top)
       end.linkTo(verticalGuideline, margin = 20.dp)
   }) {
       Text("Button 1")
   }

   Button(onClick = { }, modifier = Modifier.constrainAs(button2) {
       top.linkTo(button1.bottom)
       start.linkTo(verticalGuideline, margin = 40.dp)
   }) {
       Text("Button 2")
   }

   Button(onClick = { }, modifier = Modifier.constrainAs(button3) {
       top.linkTo(button2.bottom)
       end.linkTo(verticalGuideline, margin = 20.dp)
   }) {
       Text("Button 3")
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;h3&gt;
  
  
  Working with barriers in ConstraintLayout
&lt;/h3&gt;

&lt;p&gt;The barrier constraints are created for a specific side of one or more components using the following 4 available functions.&lt;/p&gt;

&lt;p&gt;1.createStartBarrier()&lt;br&gt;&lt;br&gt;
2.createEndBarrier()&lt;br&gt;&lt;br&gt;
3.createTopBarrier()&lt;br&gt;&lt;br&gt;
4.createBottomBarrier()&lt;br&gt;&lt;br&gt;
Each of the functions accepts the reference of the component that needs to create a barrier with optional margin parameters.&lt;/p&gt;

&lt;p&gt;val barrier = createEndBarrier(button1, button2, margin = 30.dp)&lt;/p&gt;

&lt;p&gt;createEndBarrier() function will create the vertical barrier positioned 30 dp from the widest width of button1 or button2. So start and the end is used to create vertical barriers and the top and bottom are used for horizontal barriers. &lt;/p&gt;

&lt;p&gt;To learn about barriers, let's create a sample code to understand scenarios. Implement the following sample code.&lt;/p&gt;

&lt;p&gt;ConstraintLayout(&lt;br&gt;
   modifier = Modifier&lt;br&gt;
       .size(300.dp, 200.dp)&lt;br&gt;
       .padding(10.dp)&lt;br&gt;
) {&lt;/p&gt;

&lt;p&gt;val (button1, button2, button3) = createRefs()&lt;/p&gt;

&lt;p&gt;Button(onClick = { }, modifier = Modifier&lt;br&gt;
       .constrainAs(button1) {&lt;br&gt;
           top.linkTo(parent.top)&lt;br&gt;
           start.linkTo(parent.start)&lt;br&gt;
       }&lt;br&gt;
       .width(100.dp)) {&lt;br&gt;
       Text("Button 1")&lt;br&gt;
   }&lt;/p&gt;

&lt;p&gt;Button(onClick = { }, modifier = Modifier&lt;br&gt;
       .constrainAs(button2) {&lt;br&gt;
           top.linkTo(button1.bottom, margin = 20.dp)&lt;br&gt;
           start.linkTo(parent.start)&lt;br&gt;
       }&lt;br&gt;
       .width(100.dp)) {&lt;br&gt;
       Text("Button 2")&lt;br&gt;
   }&lt;/p&gt;

&lt;p&gt;Button(onClick = { }, modifier = Modifier.constrainAs(button3) {&lt;br&gt;
       linkTo(parent.top, parent.bottom)&lt;br&gt;
       linkTo(button1.end, parent.end, startMargin = 10.dp)&lt;br&gt;
       width = Dimension.fillToConstraints&lt;br&gt;
       height = Dimension.fillToConstraints&lt;/p&gt;

&lt;p&gt;}) {&lt;br&gt;
       Text("Button 3")&lt;br&gt;
   }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;In the above example, button 3 is arranged to the right-hand side of button 1. And button3 needed its size to fill with available maximum free space. Also need to ensure that if the size of button1 and button2 is changed button3 should be changed in response to them.&lt;/p&gt;

&lt;p&gt;In the above code sample if button1’s size is set to 200. dp which is increased its width then button3’s size will get changed see the below screenshot.&lt;/p&gt;

&lt;p&gt;Now, if button2’s width changes that will not affect button3’s size but it affect that button2 overlapped by button3 as you can see in the below screenshot.&lt;/p&gt;

&lt;p&gt;Clearly, this does not look good. Isn’t it? This is happening because button3 is constrained by only button1 and is not affected by changes made on button2. To solve the above issue we need to set button3 to the right-hand side of both button1 and button3. We can achieve this by using barrier constraints.&lt;/p&gt;

&lt;p&gt;Update the above sample code with the below-given code.&lt;br&gt;
ConstraintLayout(&lt;br&gt;
   modifier = Modifier&lt;br&gt;
       .size(300.dp, 200.dp)&lt;br&gt;
       .padding(10.dp)&lt;br&gt;
) {&lt;/p&gt;

&lt;p&gt;val (button1, button2, button3) = createRefs()&lt;br&gt;
   val barrier = createEndBarrier(button1, button2)&lt;/p&gt;

&lt;p&gt;Button(onClick = { }, modifier = Modifier&lt;br&gt;
       .constrainAs(button1) {&lt;br&gt;
           top.linkTo(parent.top)&lt;br&gt;
           start.linkTo(parent.start)&lt;br&gt;
       }&lt;br&gt;
       .width(100.dp)) {&lt;br&gt;
       Text("Button 1")&lt;br&gt;
   }&lt;/p&gt;

&lt;p&gt;Button(onClick = { }, modifier = Modifier&lt;br&gt;
       .constrainAs(button2) {&lt;br&gt;
           top.linkTo(button1.bottom, margin = 20.dp)&lt;br&gt;
           start.linkTo(parent.start)&lt;br&gt;
       }&lt;br&gt;
       .width(180.dp)) {&lt;br&gt;
       Text("Button 2")&lt;br&gt;
   }&lt;/p&gt;

&lt;p&gt;Button(onClick = { }, modifier = Modifier.constrainAs(button3) {&lt;br&gt;
       linkTo(parent.top, parent.bottom)&lt;br&gt;
       linkTo(button1.end, parent.end, startMargin = 10.dp)&lt;br&gt;
       start.linkTo(barrier, margin = 10.dp)&lt;br&gt;
       width = Dimension.fillToConstraints&lt;br&gt;
       height = Dimension.fillToConstraints&lt;br&gt;
   }) {&lt;br&gt;
       Text("Button 3")&lt;br&gt;
   }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;If you have any queries regarding Android app development connect with our &lt;a href="https://www.bestremoteteam.com/hire-android-app-developers/"&gt;Android app developers&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
      <category>layouts</category>
      <category>jetpack</category>
      <category>compose</category>
    </item>
    <item>
      <title>Unity Visual Effect Graph Guide - Everything That You Need to Know</title>
      <dc:creator>Ramsingh Manek</dc:creator>
      <pubDate>Thu, 23 Mar 2023 11:05:12 +0000</pubDate>
      <link>https://dev.to/bestremoteteam/unity-visual-effect-graph-guide-everything-that-you-need-to-know-2p5b</link>
      <guid>https://dev.to/bestremoteteam/unity-visual-effect-graph-guide-everything-that-you-need-to-know-2p5b</guid>
      <description>&lt;p&gt;Unity's Visual Effect Graph (VFX Graph) is a powerful tool that enables game developers to create stunning particle effects that enhance the visual fidelity of their games.&lt;/p&gt;

&lt;p&gt;It provides a flexible and customizable framework that allows for the creation of advanced particle systems that can be used to add depth, detail, and immersion to games.&lt;/p&gt;

&lt;p&gt;In this blog post, we'll explore how to create advanced particle systems using Unity's VFX Graph.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is a Particle System?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A particle system is a collection of small, dynamic objects, known as particles, that are used to simulate various effects, such as fire, smoke, explosions, and more.&lt;/p&gt;

&lt;p&gt;A particle system consists of a set of rules that govern how particles are generated, their lifespan, movement, color, and other properties.&lt;/p&gt;

&lt;p&gt;The rules are defined by a set of parameters that can be adjusted to create different effects.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is Unity's Visual Effect Graph?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Unity's Visual Effect Graph (VFX Graph) is a powerful tool that allows developers to create complex particle systems with a high degree of control and flexibility.&lt;/p&gt;

&lt;p&gt;It is a node-based visual scripting tool that enables developers to create stunning particle effects quickly and easily. &lt;/p&gt;

&lt;p&gt;VFX Graph is built on top of Unity's High Definition Render Pipeline (HDRP), which provides enhanced visual quality and rendering capabilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Creating a Simple Particle System&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To get started with VFX Graph, let's create a simple particle system that emits particles from a point in space. &lt;/p&gt;

&lt;p&gt;Here's how:&lt;br&gt;&lt;br&gt;
↪Create a new Unity project and import the HDRP package if it's not already installed.&lt;/p&gt;

&lt;p&gt;↪Create a new VFX Graph asset by right-clicking in the Project window and selecting Create &amp;gt; Visual Effects &amp;gt; Visual Effect Graph.&lt;/p&gt;

&lt;p&gt;↪Open the VFX Graph asset by double-clicking it in the Project window. This will open the VFX Graph editor.&lt;/p&gt;

&lt;p&gt;↪In the VFX Graph editor, right-click in the graph view and select Create Node &amp;gt; Context &amp;gt; Spawn.&lt;/p&gt;

&lt;p&gt;↪In the Spawn node, set the Spawn Count parameter to 10 and the Spawn Rate parameter to 1.&lt;/p&gt;

&lt;p&gt;↪Right-click in the graph view again and select Create Node &amp;gt; Output &amp;gt; Output Particle System.&lt;/p&gt;

&lt;p&gt;↪Connect the Spawn node to the Output Particle System node by dragging a connection from the Spawn node's output port to the Output Particle System node's input port.&lt;/p&gt;

&lt;p&gt;↪Save the VFX Graph asset by pressing Ctrl+S or by selecting File &amp;gt; Save.&lt;/p&gt;

&lt;p&gt;↪Create a new GameObject in the Scene view by right-clicking and selecting Create &amp;gt; Empty.&lt;/p&gt;

&lt;p&gt;↪Add a Visual Effect component to the GameObject by selecting Add Component &amp;gt; Effects &amp;gt; Visual Effect.&lt;/p&gt;

&lt;p&gt;↪In the Visual Effect component, select the VFX Graph asset that you just created.&lt;/p&gt;

&lt;p&gt;↪Play the scene, and you should see particles being emitted from the GameObject's position.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Advanced Particle Systems&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now that you know how to create a basic particle system in VFX Graph let's explore some more advanced techniques that you can use to create more complex effects.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Particle Trails&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Particle trails are a common effect used in games to create the illusion of movement.&lt;/p&gt;

&lt;p&gt;They are often used for things like rockets, bullets, and other fast-moving objects.&lt;/p&gt;

&lt;p&gt;To create a particle trail effect in VFX Graph, you can use the Trail Renderer node.&lt;/p&gt;

&lt;p&gt;Here's how:&lt;/p&gt;

&lt;p&gt;↪In the VFX Graph editor, right-click in the graph view and select Create Node &amp;gt; Renderer &amp;gt; Trail Renderer.&lt;/p&gt;

&lt;p&gt;↪Connect the Trail Renderer node to the Spawn node by dragging a connection from the Spawn node's output port to the Trail Renderer node's input port.&lt;/p&gt;

&lt;p&gt;↪In the Trail Renderer node, set the Trail Length parameter to 1 and adjust other parameters to your liking.&lt;/p&gt;

&lt;p&gt;↪Save the VFX Graph asset and play the scene.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Particle Collisions&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Particle collisions can add a sense of realism to particle systems by creating interactions between particles and other objects in the scene.&lt;/p&gt;

&lt;p&gt;To create a particle collision effect in VFX Graph, you can use the Collision node.&lt;/p&gt;

&lt;p&gt;Here's how:&lt;/p&gt;

&lt;p&gt;↪In the VFX Graph editor, right-click in the graph view and select Create Node &amp;gt; Context &amp;gt; Collision.&lt;/p&gt;

&lt;p&gt;↪Connect the Collision node to the Spawn node by dragging a connection from the Spawn node's output port to the Collision node's input port.&lt;/p&gt;

&lt;p&gt;↪In the Collision node, adjust the Collision Type and other parameters to your liking.&lt;/p&gt;

&lt;p&gt;↪Save the VFX Graph asset and play the scene.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Particle Textures&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Using textures in particle systems can create more interesting and detailed effects.&lt;/p&gt;

&lt;p&gt;To use textures in VFX Graph, you can use the Texture Sheet Animation node.&lt;/p&gt;

&lt;p&gt;Here's how:&lt;/p&gt;

&lt;p&gt;↪In the VFX Graph editor, right-click in the graph view and select Create Node &amp;gt; Texture &amp;gt; Texture Sheet Animation.&lt;/p&gt;

&lt;p&gt;↪Connect the Texture Sheet Animation node to the Spawn node by dragging a connection from the Spawn node's output port to the Texture Sheet Animation node's input port.&lt;/p&gt;

&lt;p&gt;↪In the Texture Sheet Animation node, select a texture sheet and adjust other parameters to your liking.&lt;/p&gt;

&lt;p&gt;↪Save the VFX Graph asset and play the scene.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Particle Forces&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Using forces in particle systems can create more dynamic and realistic effects.&lt;/p&gt;

&lt;p&gt;To apply forces to particles in VFX Graph, you can use the Force Field node.&lt;/p&gt;

&lt;p&gt;Here's how:&lt;/p&gt;

&lt;p&gt;↪In the VFX Graph editor, right-click in the graph view and select Create Node &amp;gt; Context &amp;gt; Force Field.&lt;/p&gt;

&lt;p&gt;↪Connect the Force Field node to the Spawn node by dragging a connection from the Spawn node's output port to the Force Field node's input port.&lt;/p&gt;

&lt;p&gt;↪In the Force Field node, adjust the Force Type, Force Strength, and other parameters to your liking.&lt;/p&gt;

&lt;p&gt;↪Save the VFX Graph asset and play the scene.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Custom Shaders&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To create truly unique and customized particle effects, you can use custom shaders in your VFX Graph.&lt;/p&gt;

&lt;p&gt;You can create a custom shader that defines how particles are rendered and then use it in your VFX Graph to create the desired effect.&lt;/p&gt;

&lt;p&gt;Here's how:&lt;/p&gt;

&lt;p&gt;↪Create a new shader asset by right-clicking in the Project window and selecting Create &amp;gt; Shader.&lt;/p&gt;

&lt;p&gt;↪Write a custom shader using ShaderLab syntax that defines how particles are rendered.&lt;/p&gt;

&lt;p&gt;↪In the VFX Graph editor, right-click in the graph view and select Create Node &amp;gt; Renderer &amp;gt; Custom.&lt;/p&gt;

&lt;p&gt;↪Connect the Custom node to the Spawn node by dragging a connection from the Spawn node's output port to the Custom node's input port.&lt;/p&gt;

&lt;p&gt;↪In the Custom node, select the custom shader asset you created earlier.&lt;/p&gt;

&lt;p&gt;↪Save the VFX Graph asset and play the scene.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Audio Reactivity&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You can use audio reactivity in your particle systems to create effects that respond dynamically to the game's sound effects and music.&lt;/p&gt;

&lt;p&gt;To create an audio-reactive particle system in VFX Graph, you can use the Audio Spectrum node.&lt;/p&gt;

&lt;p&gt;Here's how:&lt;/p&gt;

&lt;p&gt;↪In the VFX Graph editor, right-click in the graph view and select Create Node &amp;gt; Context &amp;gt; Audio Spectrum.&lt;/p&gt;

&lt;p&gt;↪Connect the Audio Spectrum node to the Spawn node by dragging a connection from the Spawn node's output port to the Audio Spectrum node's input port.&lt;/p&gt;

&lt;p&gt;↪In the Audio Spectrum node, adjust the parameters to your liking.&lt;/p&gt;

&lt;p&gt;↪Save the VFX Graph asset and play the scene with audio.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Unity's Visual Effect Graph provides a powerful and flexible framework for creating advanced particle systems that can add depth, detail, and immersion to your games.&lt;/p&gt;

&lt;p&gt;By using techniques like particle trails, collisions, textures, and forces, you can create stunning visual effects that enhance the overall quality of your game.&lt;/p&gt;

&lt;p&gt;So go ahead and experiment with Unity's VFX Graph to create your amazing particle systems!&lt;/p&gt;

&lt;p&gt;Hope you like the Article, If you have any queries you can connect with our &lt;a href="https://www.bestremoteteam.com/hire-unity-developers/"&gt;&lt;strong&gt;Unity Developers&lt;/strong&gt;&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>unity3d</category>
    </item>
  </channel>
</rss>
