Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of constraint layout 0dp

I've been trying to achieve the following layout with compose:

enter image description here

For this, I've created the composable:

@Preview(showBackground = true)
@Composable
fun element() {
    ConstraintLayout(
        modifier = Modifier.fillMaxWidth()
    ) {
        val (checkbox, title, icon) = createRefs()

        Text(
            text = "This would be some text",
            style = TextStyle(
                color = Color.Black,
                fontSize = 18.sp,
            ),
            modifier = Modifier.constrainAs(title) {
                top.linkTo(parent.top)
                bottom.linkTo(parent.bottom)
                start.linkTo(checkbox.end)
                end.linkTo(icon.start)
            },
        )

        Checkbox(
            checked = false,
            modifier = Modifier.constrainAs(checkbox) {
                top.linkTo(title.top)
                bottom.linkTo(title.bottom)
                start.linkTo(parent.start)
            },
            onCheckedChange = {},
        )

        Icon(
            asset = Icons.Filled.Close,
            modifier = Modifier
                .constrainAs(icon) {
                    top.linkTo(title.top)
                    bottom.linkTo(title.bottom)
                    end.linkTo(parent.end)
                }
        )
    }
}

However, the text composable does not fill the entire space and the UI looks like: enter image description here

I've tried adding modifiers to the Text composable like Modifier..fillMaxWidth(), but this results in:

enter image description here

I've tried also to use a constraint set with a horizontal chain, but to no avail. I know that removing end.linkTo(icon.start) would look like this is achievable, but when the text would be really long it would overlap with the delete icon.

What am I missing here? How do I achieve the same result as in the view system when we say the TextView's width is 0dp?

like image 858
Fred Avatar asked Oct 12 '20 07:10

Fred


People also ask

What does 0dp mean in constraint layout?

for the children of ConstraintLayout if you have set constraints then the 0dp is for match_constraint (take full width, or full height) Using 0dp, which is the equivalent of "MATCH_CONSTRAINT" https://developer.android.com/reference/android/support/constraint/ConstraintLayout.

Which is better constraint layout or RelativeLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI.

What is the difference between relative layout and constraint layout?

Unlike RelativeLayout , ConstraintLayout offers a bias value that is used to position a view in terms of 0% and 100% horizontal and vertical offset relative to the handles (marked with a red circle). These percentages (and fractions) offer seamless positioning of the view across different screen densities and sizes.

What is a constraint layout?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.


1 Answers

Use Dimension.fillToConstraints:

A Dimension that spreads to match constraints. Links should be specified from both sides corresponding to this dimension, in order for this to work.

Add this line to your Text modifier:

width = Dimension.fillToConstraints

So it becomes:

Text(
    text = "This would be some text",
    style = TextStyle(
        color = Color.Black,
        fontSize = 18.sp,
    ),
    modifier = Modifier.constrainAs(title) {
        top.linkTo(parent.top)
        bottom.linkTo(parent.bottom)
        start.linkTo(checkbox.end)
        end.linkTo(icon.start)
        width = Dimension.fillToConstraints
    },
)
like image 51
Saurabh Thorat Avatar answered Sep 28 '22 02:09

Saurabh Thorat