Arsenius
Arsenius

Reputation: 5652

Jetpack Compose align input text in TextField

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?

Upvotes: 37

Views: 19857

Answers (1)

Phil Dukhov
Phil Dukhov

Reputation: 87804

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.

Upvotes: 75

Related Questions