3

I am trying to create a row of two buttons in Jetpack Compose, and I want each button to wrap its own content, which is the text "HELLO". I have achieved this by using the .defaultMinSize() modifier on each button. However, I am having trouble getting the row to wrap around its content, which is the two buttons. I have used the .wrapContentSize() modifier on the Row composable, but the height of the row is not adjusting to the height of the buttons as shown in the image.

Row(
        horizontalArrangement = Arrangement.SpaceEvenly,
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .wrapContentSize()
            .background(LightGrey)
    ) {
        Button(
            modifier = Modifier
                .defaultMinSize(minWidth = 1.dp, minHeight = 1.dp)
            ,
            onClick = { /*TODO*/ },
            contentPadding = PaddingValues(0.dp)
        ) {
            Text(
                text = "HELLO",
            )
        }
        Button(
            modifier = Modifier
                .defaultMinSize(minWidth = 1.dp, minHeight = 1.dp)
            ,
            onClick = { /*TODO*/ },
            contentPadding = PaddingValues(0.dp)
        ) {
            Text(
                text = "HELLO",
            )
        }
    }

enter image description here

1 Answer 1

3

It is due to accessibility. Material components that have a visual size that is lower than the minimum touch target size for accessibility (such as Button) will include extra space outside the component to ensure that they are accessible.

If you want to remove it you have to force LocalMinimumInteractiveComponentEnforcement to false

    Row(
        modifier = Modifier
            .background(Gray)
    ) {
        CompositionLocalProvider(LocalMinimumInteractiveComponentEnforcement provides false) {
            Button()
            Button()
        }
    }

enter image description here

Note: LocalMinimumInteractiveComponentEnforcement requires at least M2 1.4.0-alpha04 and M3 1.1.0-alpha04. Before you can use LocalMinimumTouchTargetEnforcement in the same way.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.