Jetpack Compose align input text in TextField
O

1

32

I would like to achieve similar behaviour with TextField from Jetpack Compose like from old school XML layout:

<EditText
    android:id="@+id/some_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right" /> <!-- or end -->

When I try this approach:

TextField(value = "", onValueChange = {

}, textAlign = TextAlign.End) 

It simply just doesn't work because textAlign property doesn't exists in TextField. Then how to do input text alignment like TextAlign.Center, TextAlign.End, TextAlign.Justify and so on for TextField?

Obliging answered 25/7, 2021 at 9:12 Comment(0)
E
68

You can do it through textStyle.

TextField(
    value = "",
    onValueChange = {

    },
    textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.End)
)

LocalTextStyle.current is the default value for TextField, you can replace it by your own.

Ewan answered 25/7, 2021 at 12:46 Comment(2)
Thanks, then why textStyle doesn't exist for Text and textAlign doesn't exist for TextField?Obliging
In Text it's called style. I'm not sure why such design is taken, probably because changing align for TextField is not needed that often. You can set textAlign for Text the same way as for TextField, but it also has textAlign that'll override the style value.Ewan

© 2022 - 2025 — McMap. All rights reserved.