https://kotlinlang.org logo
#compose
Title
# compose
z

zalewski.se

06/21/2020, 2:18 PM
Is it possible to attach the
TextField
to the right side of the screen? I’m trying to use
ConstraintLayout
for this, where everything looks as expected with
Text
element, but when I changed it to
TextField
it looks like the:
Copy code
right constrainTo parent.right
doesn’t work 🤔 To give you some code: This is sample with `Text`:
Copy code
ConstraintLayout(
            modifier = Modifier.fillMaxWidth().wrapContentHeight(),
            constraintSet = ConstraintSet {
                tag("amountTag").apply {
                    right constrainTo parent.right
                    right.margin = 16.dp
                    horizontalBias = 1.0f
                    centerVertically()
                }
            }
    ) {
        Text(
                modifier = Modifier.tag("amountTag"),
                fontSize = 16.sp,
                text = rate.value.toString()
        )
    }
And when I just changed
Text
to `TextField`:
Copy code
TextField(
                modifier = Modifier.tag("amountTag"),
                value = value,
                onValueChange = {
                    value = it
                }
        )
Also I tried to add some modifiers to
TextField
like:
.wrapContentWidth(Alignment.End)
but same result.
a

Anastasia [G]

06/21/2020, 10:20 PM
I think this is because in that case TextField is not wrapping the input text and is actually taking more space horizontally. And within this width the text is aligned to the left. To check this, you could add a
background()
modifier to the TextField to see how much space it occupies. If this is indeed the case, then you could either try to make TextField wrap the content by setting the minimum width to something other than 0 (e.g.
preferredWidthIn(minWidth = 5.dp)
). Or you could align the input text within the TextField to the right (using the
textAlign
param of the TextStyle param of the TextField).
z

zalewski.se

06/22/2020, 2:40 PM
I see, thanks! 🙇‍♂️