Dec 10, 2023

Understanding Layouts in Android Compose

A very quick tutorial on compose and layouts.

Understanding Layouts in Android Compose

A composable function is a function that provides some Kotlin units that describe UI on the screen. For example you can write composable function that returns a textbox or an image.

@Composable 
fun ArtistCard() {    
 Text("Alfred Sisley")     
 Text("3 minutes ago") 
}      

The problem with above code however is does not tell Android how to actually display both items. Should they be displayed one after another, one below another or super imposed on each other ? For that we have layout. For the web developers among you, you will be familiar with grid based layout in HTML.

@Composable
fun ArtistCardColumn() {
    Column {
        Text("Alfred Sisley")
        Text("3 minutes ago")
    }
}

Two text elements arranged in a column layout, so the text is readableAbove code tells that two text boxes are to be displayed in a column. Column is a special construct that tells Android how it is to be rendered.

Another way to render these would be to display them in row. For that we have Row construct.

@Composable
fun ArtistCardArrangement(artist: Artist) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.End
    ) {
        Image(bitmap = artist.image, contentDescription = "Artist image")
        Column { /*...*/ }
    }
}

Items are aligned to the rightAnother common layout is Box where items are super imposed on each other.

Compares three simple layout composables: column, row, and boxYou can create more complex layouts by mixing different layouts.

@Composable
fun SearchResult() {
    Row {
        Image(
            // ...
        )
        Column {
            Text(
                // ...
            )
            Text(
                // ...
            )
        }
    }
}

Conclusion

It is really simple to understand concept of layouts in Compose. Basically you use Rows and Columns and mix them together to get what you want. There are other types of layouts too such as responsive layouts and slot based layouts too but they are more of an advanced case. Can you look at an App and guess how the layout is structured ?

Continue Reading
Learn about other front end technologies

Learn about other front end technologies

We are partnering with Frontendeng.dev to cross promote the content around front end engineering.

Published Dec 12, 2023

Android paired with Windows Laptops

Android paired with Windows Laptops

The new trend is to build Android and Windows Laptop into one hardware.

Published Jan 11, 2024

What are co-routines in Kotlin ? Kotlin for Android Part -2

What are co-routines in Kotlin ? Kotlin for Android Part -2

In this part we will learn about Kotlin's co-routines. It is an important concurrency design pattern that is extremely useful when designing asynchronous programs.

Published Dec 31, 2023

Backends for your Android App

Backends for your Android App

In this article, we will explore the various options available for app developers who are primarily focused on building an app backend.

Published Jan 1, 2024

Google Flutter vs Android Jetpack Compose : A detailed comparison

Google Flutter vs Android Jetpack Compose : A detailed comparison

Flutter code looks remarkably similar to Android Jetpack Compose. However which one of them is better ? We find out in this article.

Published Jan 11, 2024

Minimal starting template for Android Compose

Minimal starting template for Android Compose

Find a simple minimal code to open an app with top and bottom bar. Find full code on https://github.com/Wiseland-Inc/dev.androidauthority.app

Published Dec 11, 2023

Spring Boot for App Backends

Spring Boot for App Backends

Simple spring boot template to deploy on Google Cloud and to be used as your app backend.

Published Jan 6, 2024

Learning Kotlin for Android development - Part 1

Learning Kotlin for Android development - Part 1

A guide focused on teaching you basics of Kotlin just for Android development.

Published Dec 7, 2023

Making android development less painful

Making android development less painful

Android development has always been challenging and frustrating right from start. However things are looking better now.

Published Dec 4, 2023

What are adaptive android apps ?

What are adaptive android apps ?

Adaptive apps are app equivalent of responsive web apps. Apps that resize to give good user experience across several devices.

Published Dec 9, 2023