I've been currently developing and learning android jetpack compose and there are some things I don't know and wonder about this. One thing I'm wondering I'm trying to do a back button positioned in the upper left corner of the screen and a text positioned in the middle. I originally wrote this code like this:
Row(modifier = Modifier
.padding(5.dp)
.fillMaxWidth()) {
Image(
modifier = Modifier
.clickable {
onBackClick()
}
.padding(4.dp),
alignment = Alignment.CenterStart,
painter = painterResource(id = R.drawable.ic_back),
contentDescription = "back")
Row(
horizontalArrangement = Arrangement.Center){
Text(text = "Help")
}
}
Since I gave a modifier.FillMaxWidth in the first Row, I didn't give it in the second because it has a child component, I thought it was unnecessary, and I gave a horizontalArrangment center in the second Row to align the Text in the middle, but even though I did this, the back button and the text appeared side by side on the upper left.
However, when I rewrote my code like this, when I added the second Row modifier.FillMaxWidth, it worked fine.
Row(modifier = Modifier
.padding(5.dp)
.fillMaxWidth()) {
Image(
modifier = Modifier
.clickable {
onBackClick()
}
.padding(4.dp),
alignment = Alignment.CenterStart,
painter = painterResource(id = R.drawable.ic_back),
contentDescription = "back")
Row(modifier = Modifier.fillMaxWidth(), // <----
horizontalArrangement = Arrangement.Center){
Text(text = "Yardım")
}
}
I added this modifier = Modifier.fillMaxWidth() second Row component
Why is it necessary to give Modifier.fillMaxWidth() on the second Row? We already give it in the parent component? what's going on in the background?
The
horizontalArrangement
in theRow
= the horizontal arrangement of the layout's children in the available space.Using:
Using:
In any case using this layout the
Text(text = "Yardım")
is not centered in the whole 1stRow
but only in the space occupied by the 2ndRow
.If you want to center the text in the whole 1st
Row
you can use aBox
as parent container.In this case use the
align
modifier in the children to a specificAlignment
within theBox
. This alignment will have priority over theBox
'salignment
parameter.Something like: