How to create a composable that uses a ConstraintLayout in Kotlin

Oscar de la Hera Gomez
Four flowers that represent Kotlin, Android, Jetpack Compose and Android Studio side by side. Beneath them sits the text “Constraint Layout.”

A step by step guide on creating a screen or component using Jetpack Compose and Kotlin that uses constraints.

The following tutorial shows how to create a composable screen that has a constrained layout and demonstrates how you can position a box at any position using constraints.

Step One: Add the dependency

A screenshot of Android Studio showing the app level build.gradle.kts. We have highlighted how to implement the constraintlayout dependecy. The code is available below.

In the App Level, build.gradle.kts add the constraintlayout dependency using the following line:

implementation("androidx.constraintlayout:constraintlayout-compose:1.0.1")

Please note that 1.0.1 may not be the latest version, please update the code above to the latest version in your Android Studio project.

Step Two: Sync the Gradles

A screenshot of Android Studio showing a gradle file that has recently been modified. Highlighted is the "Sync Now" button that appears on the right of the prompt at the top of the Gradle file. Click this button to sync the gradle files with the project.

Press Sync Now in the bar above the Gradle to update the project dependencies.

Step Three: Create the Composable

A screenshot of Android Studio highlighting the Component and Screens folder within the UI folder. Create a file within the relevant folder and move onto the next step.

Under the ui folder, in the screens or components folder, create a new Kotlin file with the name of your composable.

Step Four: Create the base

A screenshot of Android Studio showing the sample code provided below.

Use the code below to create the base of the constrained composable.

Step Five: Add Elements

A screenshot of Android Studio showing the completed code at the end of the exercise. Follow the steps below to recreate this image.

The following step shows you how to add elements to the constrained composable.

A | Create the References

A screenshot of Android Studio showing you how to create constraintlayout references.

For each element within your ConstraintLayout, create a reference using code similar to the one below:

val (elementOne, elementTwo) = createRefs()

B | Add the Elements

A screenshot of Android Studio showing you how to add elements to the constraintlayout. Sample code provided below.

Complete the design of your component or screen by adding elements to the ConstraintLayout.

We have provided sample code to ease your development and suggest that you consult the link below to learn all the possible things that you can do with a ConstraintLayout.

If you are curious to learn how we handle localization (Text depending on language) or how we implemented our Styleguide, please consult the tutorials below.

Step Six: Add the Composable to the UI

A screenshot of Android Studio showing how we added the composable to the UI of the MainActivity.

Add the newly created Composable to your UI.

Step Seven: Test

An image showing a screenshot of an Android phone with a box centered on the screen and a label beneath it with the text "A Sample String."

Run the code and confirm that everything looks right.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details