Android
Text Alignment
Center Text
Android Development
UI Design

align text center with android

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Centering text in Android can mean two different things: centering the text inside a view, or centering the view itself inside its parent. Those are controlled by different attributes, and confusion between them is why many layouts look almost correct but still end up shifted.

gravity Versus layout_gravity

For views such as TextView, gravity controls how the text is positioned inside the view's own bounds.

xml
1<TextView
2    android:layout_width="200dp"
3    android:layout_height="80dp"
4    android:gravity="center"
5    android:text="Hello" />

This centers the text inside the TextView rectangle.

layout_gravity does something different. It asks the parent layout to position the whole view.

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="match_parent"
4    android:orientation="vertical">
5
6    <TextView
7        android:layout_width="wrap_content"
8        android:layout_height="wrap_content"
9        android:layout_gravity="center"
10        android:text="Hello" />
11</LinearLayout>

Here the TextView itself is centered inside the LinearLayout.

Centering Text In A TextView

If the goal is just "put the letters in the middle of the label," use android:gravity.

xml
1<TextView
2    android:layout_width="match_parent"
3    android:layout_height="48dp"
4    android:gravity="center"
5    android:text="Centered text" />

That centers horizontally and vertically within the view.

You can also be more specific:

xml
android:gravity="center_horizontal"
android:gravity="center_vertical"

Use those when only one axis should be centered.

Centering A View In Its Parent

In a LinearLayout, layout_gravity is the usual tool.

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="match_parent"
4    android:gravity="center">
5
6    <TextView
7        android:layout_width="wrap_content"
8        android:layout_height="wrap_content"
9        android:text="Centered view" />
10</LinearLayout>

Notice that this time the parent uses android:gravity="center". That centers all child content in the layout.

ConstraintLayout Centers By Constraints

With ConstraintLayout, centering comes from constraining both sides to the parent.

xml
1<androidx.constraintlayout.widget.ConstraintLayout
2    android:layout_width="match_parent"
3    android:layout_height="match_parent"
4    xmlns:android="http://schemas.android.com/apk/res/android"
5    xmlns:app="http://schemas.android.com/apk/res-auto">
6
7    <TextView
8        android:id="@+id/title"
9        android:layout_width="wrap_content"
10        android:layout_height="wrap_content"
11        android:text="Centered"
12        app:layout_constraintStart_toStartOf="parent"
13        app:layout_constraintEnd_toEndOf="parent"
14        app:layout_constraintTop_toTopOf="parent"
15        app:layout_constraintBottom_toBottomOf="parent" />
16</androidx.constraintlayout.widget.ConstraintLayout>

That centers the view itself. If you also want text centered inside a wider TextView, add android:gravity="center" too.

Programmatic Centering

You can set the same behavior in code.

kotlin
val label = findViewById<TextView>(R.id.title)
label.gravity = Gravity.CENTER

This is useful when alignment depends on state or screen mode.

Multi-Line Text Needs Width

A common surprise is that multi-line centering looks wrong when the view shrinks to wrap_content. If the text is multi-line and you want the lines centered as a block, the view often needs a meaningful width.

xml
1<TextView
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:gravity="center"
5    android:textAlignment="center"
6    android:text="Line one&#10;Line two" />

textAlignment can also matter for some layout and locale situations, especially with API-level-specific behavior.

When textAlignment Helps

gravity positions text within the view. textAlignment influences how the text itself is aligned, especially for multi-line content and view text direction.

xml
android:textAlignment="center"

In many simple cases gravity="center" is enough, but textAlignment is worth knowing when the visual result is not what you expect.

EditText Needs Extra Judgment

Centering user-editable text works the same technically, but it does not always feel good in forms. A centered EditText can look polished for short OTP codes or search bars, yet it can be awkward for longer free-form input because the caret and text movement are harder to scan.

xml
1<EditText
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:gravity="center"
5    android:hint="Enter code" />

So the right answer is not only about the attribute. It is also about whether centered text matches the interaction pattern of that field.

Common Pitfalls

The most common mistake is using layout_gravity when the real problem is text placement inside the view. Another is centering the text inside a tiny wrap_content view and expecting it to look like a centered block in the parent. Developers also often forget that ConstraintLayout ignores layout_gravity; it uses constraints instead. Finally, multi-line text may need both a proper width and centered text alignment to look visually correct.

Summary

  • Use gravity to center text inside a TextView.
  • Use layout_gravity or parent gravity to center the view in layouts that support it.
  • In ConstraintLayout, center by constraining both sides to the parent.
  • Multi-line centering usually needs a wider view, not just wrap_content.
  • 'gravity and textAlignment solve related but different alignment problems.'

Course illustration
Course illustration

All Rights Reserved.