Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use bias in Constraint Layout Compose

How do I set the layout_constraintHorizontal_bias prop on a Composable that is in a Constraint Layout? Here is the XML code:

<TextView
    ...
    tool:layout_constraintStart_toStartOf="parent"
    tool:layout_constraintEnd_toEndOf="parent"
    tool:layout_constraintWidth_max="wrap"
    tool:layout_constraintHorizontal_bias="0"/>

Here is how my Jetpack Compose code looks right now:

ConstraintLayout(modifier = modifier.fillMaxSize()) {
    val (button1, button2) = createRefs()
    Button(
        onClick = {},
        modifier = Modifier.constrainAs(button1) {
            top.linkTo(parent.top, margin = 16.dp)
        }
    ) {
        Text(text = "Button 1")
    }

    Button(
        onClick = {},
        modifier = Modifier.constrainAs(button2) {
            top.linkTo(button1.bottom, margin = 4.dp)
            start.linkTo(button1.end, margin = 20.dp)
            end.linkTo(parent.end, margin = 20.dp)
            width = Dimension.preferredWrapContent
        }
    ) {
        Text(text = "Button 2")
    }
}

So my question is how do I set the horizontal bias of Button 2 to be 0?

like image 856
Ivan Šimović Avatar asked Oct 02 '20 12:10

Ivan Šimović


People also ask

What is constraint Layout bias?

Bias, in terms of ConstraintLayout , means "if there is extra room, slide the widget in this direction along the axis". The default bias is 0.5, meaning that the widget is centered in the available space.

How can we use barrier in constraint Layout?

To define a Barrier , you can select one or more View components from the “Design” view, open the “Guidelines” menu and select the Barrier . If you want to add it directly in the XML, you can use the following code snippet: The resulting layout looks like the screenshot of the “Design” layout editor view from below.

What is vertical bias in constraint Layout Android?

The value you set as a horizontal or vertical bias is a number between 0 and 1, representing a percentage, where the closest to 0 means the more biased to the left (horizontal) or the top constraint (vertical) and the closest to 1 means the more biased to the right (horizontal) or the bottom constraint (vertical).

How can we solve missing constraints in constraint Layout?

These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.


Video Answer


2 Answers

You have to use the linkTo function of the ConstrainScope which has more parameters:

ConstraintLayout(modifier = modifier.fillMaxSize()) {
    val (button1, button2) = createRefs()
    Button(
        onClick = {},
        modifier = Modifier.constrainAs(button1) {
            top.linkTo(parent.top, margin = 16.dp)
        }
    ) {
        Text(text = "Button 1")
    }

    Button(
        onClick = {},
        modifier = Modifier.constrainAs(button2) {              
            top.linkTo(button1.bottom, margin = 4.dp)
            linkTo(button1.end, parent.end, startMargin = 20.dp, endMargin = 20.dp, bias = 0F)
            width = Dimension.preferredWrapContent
        }
    ) {
        Text(text = "Button 2")
    }
}
like image 75
gookman Avatar answered Oct 06 '22 00:10

gookman


I guess that ConstraintLayout in Jetpack Compose is not on par with the xml one yet and bias is simply missing. I found a workaround though - you can create a chain and chains actually do support bias (speaking about version 1.0.0-alpha04).

For your example, something like this should do the trick:

ConstraintLayout(modifier = modifier.fillMaxSize()) {
    // ...

    createHorizontalChain(button2, chainStyle = ChainStyle.Packed(0F))
}
like image 42
Jan Bína Avatar answered Oct 06 '22 01:10

Jan Bína