Search code examples
user-interfacetextfielduser-experienceandroid-jetpack-composetext-alignment

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?


Solution

  • 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.