Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap content but with minimum width/height - Jetpack Compose

I have a Box and some Text in it. The Text is actually just a number, which can be 1, 2, or 3 digits. On the one hand, I want to wrap the content to accommodate for the case where we get a 3 digit number (which would make the box wider), but on the other hand, if we get a 1 digit number, the box becomes very skinny and looks visually awkward.

I've tried Modifier.width(Int.dp) and Modifier.wrapContentWidth() and even a combination of the two, but can't get it to behave right. Is there a way in Compose to wrap the content, but with a minimum (default) width or height?

I'd rather not use an if - else statement on the input size if at all possible, since that would sort of defeat the purpose.

like image 738
kc_dev Avatar asked Dec 06 '25 21:12

kc_dev


1 Answers

You may use the widthIn modifier to specify a minimum width, while allowing the element to expand further if necessary.

@Composable
fun Foo(){
    Text(text = "1", modifier = Modifier.widthIn(min = 32.dp))
}
like image 165
Adrian K Avatar answered Dec 08 '25 13:12

Adrian K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!