3

Trying to achieve 3 items in row equally distributed horizontally with option to show/hide some of them in compose v1.3.3.

If it's a simple if statement to include view in compose all works fine (without animation)

Row(
    modifier = modifier.fillMaxWidth(),
    verticalAlignment = Alignment.CenterVertically
) {
    Text(text = "abc", modifier = modifier.weight(1f))
    if (false) {
        Text(text = "def", modifier = modifier.weight(1f))
    }
    if(true) {
        Text(text = "zxc", modifier = modifier.weight(1f))
    }
}

Items without animation based on condition

As soon as item wrapped with AnimatedVisibility as condition UI becomes broken.

Row(
    modifier = modifier.fillMaxWidth(),
    verticalAlignment = Alignment.CenterVertically
) {
    Text(text = "abc", modifier = modifier.weight(1f))
    AnimatedVisibility(visible = false) {
        Text(text = "def", modifier = modifier.weight(1f))
    }
    AnimatedVisibility(visible = true) {
        Text(text = "zxc", modifier = modifier.weight(1f))
    }
}

enter image description here

Seems like visibility + size changes same time could ruin the UI.

Is there a way for smooth animation without implementing very custom ones?

1 Answer 1

6

The problem here is that the weight modifier should be on the direct childs of row.

In the second case using AnimatedVisibility you should use modifier on AnimatedVisibility and not on Text anymore.


    Row(
        modifier = Modifier.fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(text = "abc", modifier = Modifier.weight(1f))
        AnimatedVisibility(visible = false, modifier = Modifier.weight(1f)) {
            Text(text = "def")
        }
        AnimatedVisibility(visible = true, modifier = Modifier.weight(1f)) {
            Text(text = "zxc")
        }
    }

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.