Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
Compose Layout: Align relative to centered item (eg. toRightOf)

Is there a way in Compose to align a composable next to a centered item without using ConstraintLayout?

I want to achieve this: enter image description here

I could use a Spacer and Weights like this enter image description here

Row(
    modifier = Modifier.fillMaxWidth(),
    horizontalArrangement = Arrangement.Center,
    verticalAlignment = Alignment.CenterVertically,
) {

    Spacer(Modifier.weight(1f))
    Button(...)
    Label(Modifier.weight(1f),...)
}

Problem is that I display the Label conditionally and if I hide the two elements with the weights, the button moves slightly.

Also not sure if using weights is producing more performance impact than the ConstraintLayout in the first place.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The Modifier.weight is the right way to create such a layout. I'm not sure if the performance is better than ConstraintLayout, but certainly not worse.

If you run into performance problems, you should create a problem on the google issue tracker, since that's how Compose is supposed to be used. Personally, I haven't encountered any performance problems related to weight, but the technology is fairly recent, so you can't completely rule out such a possibility.

In your case, you need to have some representation at any given time, to which you can apply Modifier.weight. You can use if-else and add Spacerinelsecase, but I prefer to useBox` with optional content: it looks cleaner and will work correctly in case you add animation.

Default Box contentAlignment is Alignment.TopStart, which will work exactly as needed in your case, but in some other cases, you can override it or add more Spacers inside.

Row(
    modifier = Modifier.fillMaxWidth(),
    horizontalArrangement = Arrangement.Center,
    verticalAlignment = Alignment.CenterVertically,
) {
    Spacer(Modifier.weight(1f))
    Button(...)
    Box(Modifier.weight(1f)) { 
        if (condition) {
            Label(...)
        }
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!