0

In Android compose is there anyway to center text within a TextField?

    TextField(
    value = text,
    onValueChange = setText,
    maxLines = 2,
    modifier = modifier
        .clip(RoundedCornerShape(10.dp))
        .background(Color(0xffe2ebf0))
        .border(BorderStroke(5.dp, color = MaterialTheme.colors.secondary), shape = RoundedCornerShape(1.dp)),
    textStyle = MaterialTheme.typography.h3,
    keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done),
    keyboardActions = KeyboardActions(onDone = { submit() })

)

Cannot post images yet... So here is a link!

Gabriele Mariotti
  • 192,671
  • 57
  • 469
  • 489
Lucas
  • 1
  • 1

2 Answers2

0

You can use textAlign = TextAlign.Center in the TextStyle.

Something like:

TextField(
    //...your code
    textStyle = MaterialTheme.typography.h3.copy(textAlign = TextAlign.Center),
)

enter image description here

Gabriele Mariotti
  • 192,671
  • 57
  • 469
  • 489
-2

Go to xml file and set android:gravity="center" for EditText

sample example :

<EditText
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
    android:text="some text"
/>
Abhay
  • 77
  • 1
  • 4