DEV Community

Cover image for πŸš€ Jetpack Compose Layouts
Spikey sanju
Spikey sanju

Posted on

πŸš€ Jetpack Compose Layouts

Howdy Android DevsπŸ‘‹, This is my first article for Android πŸ₯°. In this article, we gonna see the basics of Layout in Jetpack compose πŸ“’ & How to use them πŸ’‘.

Β 

Before jumping πŸƒπŸ»β€β™‚οΈ into the topic let's learn about Jetpack compose first!

Β 

What is Jetpack Compose πŸ€”?

  • Jetpack Compose is Android’s modern toolkit for building native UI.

  • It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs.

Β 

Some Interesting facts below πŸ‘‡

Screenshot 1942-08-02 at 10.43.17

Β 

Now let's learn about layouts in compose πŸ˜‹

Jetpack compose have various layouts in this article we gonna the see about standard layouts πŸ‘‡

  1. Column
  2. Row
  3. Box
  4. Constraint etc...

Β 

Now let's see how to use the above standard layout in compose πŸ‘

Β 

What is the Column Layout?

Use Column to place items vertically on the screen. It's like a LinearLayout (Vertical Orientation) in Android XML.

Β 

Example for Column LayoutπŸ’‘

@Composable
fun ProfileCard() {
  Column {
    Text("Spikey Sanju")
    Text("Android Developer & UI/UX Designer")
  }
}
Enter fullscreen mode Exit fullscreen mode

Β 

In this above example, We have added the @Composable annotation to create the Composable function.

  1. Composable functions can be only called from another Composable function.

  2. Composable functions are the functions that we use to draw the UI on the screen. All the widgets written inside it is rendered on the screen.

Β 

Now let's take a look inside the sample code πŸ•΅πŸ»β€β™‚οΈ

We are placing two text components inside the Column layout. Now the Column layout will place all the items inside the Column Vertically

Β 

Now let's see the output πŸ₯³

ROWS & COLUMNS

Okay, this is how the Column works πŸ˜„. Now let's take a look at the Row Layout πŸ‘

Β 

What is the Row Layout?

Use Row to place items horizontally on the screen. It's like a LinearLayout (Horizontal Orientation) in Android XML.

Β 

Example for Row LayoutπŸ’‘

@Composable
fun ProfileCard() {
  Row{

// getting the image from the drawable
            Image(modifier = modifier.preferredSize(60.dp).clip(CircleShape).align(Alignment.CenterVertically),
                    asset = imageResource(id = R.drawable.spikeysanju),
                    contentScale = ContentScale.Crop)


// used to give horizontal spacing between components
            Spacer(modifier = modifier.width(16.dp))

            Column(modifier = modifier.align(Alignment.CenterVertically)) {
                Text(text = "Spikey Sanju", color = Color.Black, style = typography.h6)

// used to give vertical spacing between components
                Spacer(modifier = modifier.height(8.dp))
                Text(text = "Android Developer & UI/UX Designer", color = Color.DarkGray, style = typography.caption)
            }
        }

}
Enter fullscreen mode Exit fullscreen mode

Β 

In the above code, we have used Modifiers. Let's explore the modifiers πŸ‘¨πŸ»β€πŸ’»

Β 

What are Modifiers in Compose πŸ˜‹

Modifiers allow you to tweak how a composable is presented. Modifiers let you do these sorts of things:

Β 

  1. Change the composable's behavior and appearance
  2. Add information, like accessibility labels
  3. Process user input
  4. Add high-level interactions, like making an element clickable, scrollable, draggable, or zoomable

Β 

Modifiers are standard Kotlin objects. Create a modifier by calling one of the Modifier class functions. You can chain these functions together to compose them:

Β 

Now let's see the output for Row Layout πŸ₯³

ROWS & COLUMNS

Β 

Let's see the blueprint of the above examples for Rows & Columns ℹ️

Β 

Alt Text

Β 

Now you can understand how the layout is combined & arranged.
This is how you have to combine Rows & Columns to create various layout according to your needs.

Β 

In the next article, we will see how to use Box Layout, Constraint Layout & much more fun kinds of stuff in jetpack compose ... Stay tuned πŸ₯³πŸ€˜...

Comment down your thoughts below πŸ‘‡

Top comments (2)

Collapse
 
levirs565 profile image
Levi Rizki Saputra

Great postπŸ‘

Collapse
 
spikeysanju profile image
Spikey sanju

Thank you!