Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading