https://kotlinlang.org logo
#compose
Title
# compose
y

yahyabayramoglu

03/05/2021, 9:20 PM
Is there an easy way to center a composable inside a
Card
? 😅 Card has fixed size, and it only contains a Text which needs to be centered. This is literally the only way I could get it work - I must be doing something wrong here 🙂
Copy code
Card(modifier = Modifier.size(50.dp).padding(4.dp)) {
    Row(verticalAlignment = Alignment.CenterVertically) {
          Text(
                text = "10",
                textAlign = TextAlign.Center,
                fontSize = 10.sp,
                modifier = Modifier.fillMaxWidth()
           )
     }
}
m

Mehdi Haghgoo

03/05/2021, 9:34 PM
You can replace your Row with a box that can in turn have a Center alignment.
Copy code
Box(modifier = Modifier, contentAlignment = Alignment.Center) {
        Text(
            text = "10",
            textAlign = TextAlign.Center,
            fontSize = 10.sp,
            modifier = Modifier.fillMaxWidth()
        )
}
y

yahyabayramoglu

03/05/2021, 9:49 PM
I'd rather not have any other additional composable just for the sake of centering 😞
l

Louis Pullen-Freilich [G]

03/05/2021, 10:10 PM
Maybe you are just looking for:
Copy code
Card(modifier = Modifier.size(50.dp).padding(4.dp)) {
            Text(
                text = "10",
                fontSize = 10.sp,
                modifier = Modifier.wrapContentSize()
            )
        }
👍 1
y

yahyabayramoglu

03/05/2021, 10:14 PM
Yes, that seems to do the trick 🙂 Thanks!