46

How to use Spacer filling between with two elements in a row, let one element at the start of row and another at the end?

Row { Text("Start") Spacer(modifier = Modifier.SpaceBetween) // How to set the modifier Text("End") } 

4 Answers 4

88

The Modifier.SpaceBetween doesn't exist.

You can use the horizontalArrangement parameter in the Row applying theArrangement.SpaceBetween.This parameter places children such that they are spaced evenly across the main axis, without free space before the first child or after the last child.

Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween ) { Text("Start") Text("End") } 

As alternative you can apply the weight(1f) to the Spacer.
Something like:

Row (modifier = Modifier.fillMaxWidth()) { Text("Start") Spacer(Modifier.weight(1f)) Text("End") } 

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

What if my 'Start' Text is long enough that it reaches till 'End' Text. How to limit 'Start' width to be limited till 'End' starts
@Ali_Waris In this case you can use Arrangement.SpaceBetween applying a weight modifier to the first text.
Why doesn't it work properly when I don't add fillmaxwidth?I was just wondering why
@NewPartizal Because the horizontalArrangement places children according to the available space.
19

You can use this:

Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween ) { Text("Start") Text("End") } 

Comments

14

The possible horizontal arrangements are following. I added 3 elements for better clarity.

Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly ) { TextButtons... } 

Space evenly

Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween ) { TextButtons... } 

Space Between

Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceAround ) { TextButtons... } 

enter image description here

1 Comment

Also we can set custom space between with horizontalArrangement = Arrangement.spacedBy(4.dp)
-2

Using Box can give spacing of exactly the amount of dp you specify.

Row { Text(text = "1") Box(modifier = Modifier.width(10.dp)) // gives horizontal spacing of 10 dp Text(text = "1") } 

Same goes for Column

 Column { Text(text = "1") Box(modifier = Modifier.height(10.dp)) // gives vertical spacing of 10 dp Text(text = "1") } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.