Android
TextView
Right Align
Android Development
Text Formatting

Right align text in android TextView

Master System Design with Codemia

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

Introduction

To right-align text in an Android TextView, set android:gravity="right" (or end for RTL support) in XML, or call setGravity(Gravity.END) in code. The gravity attribute controls the alignment of content within the view's bounds, while layout_gravity controls the view's position within its parent. For proper right-to-left (RTL) language support, prefer end over rightend adapts automatically based on the layout direction.

XML: gravity Attribute

xml
1<!-- Right-align text within the TextView -->
2<TextView
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:gravity="right"
6    android:text="Right aligned text" />
7
8<!-- RTL-aware: use "end" instead of "right" -->
9<TextView
10    android:layout_width="match_parent"
11    android:layout_height="wrap_content"
12    android:gravity="end"
13    android:text="Right aligned text" />
14
15<!-- Vertical and horizontal alignment -->
16<TextView
17    android:layout_width="match_parent"
18    android:layout_height="100dp"
19    android:gravity="end|center_vertical"
20    android:text="Right and vertically centered" />

The TextView must have enough width for alignment to be visible. With wrap_content width, the view is only as wide as the text, so alignment has no visible effect.

Programmatic: setGravity()

kotlin
1// Kotlin
2val textView = findViewById<TextView>(R.id.myTextView)
3textView.gravity = Gravity.END  // RTL-aware right alignment
4
5// Right + vertically centered
6textView.gravity = Gravity.END or Gravity.CENTER_VERTICAL
java
1// Java
2TextView textView = findViewById(R.id.myTextView);
3textView.setGravity(Gravity.END);
4
5// Right + vertically centered
6textView.setGravity(Gravity.END | Gravity.CENTER_VERTICAL);

gravity vs layout_gravity

These two attributes serve different purposes:

xml
1<!-- gravity: aligns CONTENT inside the view -->
2<TextView
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:gravity="end"
6    android:text="Text is right-aligned inside the full-width view" />
7
8<!-- layout_gravity: positions the VIEW inside its parent -->
9<TextView
10    android:layout_width="wrap_content"
11    android:layout_height="wrap_content"
12    android:layout_gravity="end"
13    android:text="View itself is positioned at the right of parent" />

For text alignment within the TextView, use gravity. For positioning the TextView within a FrameLayout or LinearLayout, use layout_gravity.

textAlignment Attribute (API 17+)

textAlignment provides finer control and is specifically for text alignment:

xml
1<TextView
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:textAlignment="viewEnd"
5    android:text="Aligned to view end" />

Available values:

xml
1<!-- Inherit from parent -->
2android:textAlignment="inherit"
3
4<!-- Align to gravity (default) -->
5android:textAlignment="gravity"
6
7<!-- Align to start/end of text direction -->
8android:textAlignment="textStart"
9android:textAlignment="textEnd"
10
11<!-- Align to start/end of view -->
12android:textAlignment="viewStart"
13android:textAlignment="viewEnd"
14
15<!-- Center -->
16android:textAlignment="center"

textAlignment takes priority over gravity when both are set (API 17+).

Right-to-Left (RTL) Support

xml
1<!-- Enable RTL support in AndroidManifest.xml -->
2<application
3    android:supportsRtl="true"
4    ... >
5
6<!-- Use "start" and "end" instead of "left" and "right" -->
7<TextView
8    android:layout_width="match_parent"
9    android:layout_height="wrap_content"
10    android:gravity="end"
11    android:paddingStart="16dp"
12    android:paddingEnd="16dp"
13    android:text="Adapts to LTR and RTL layouts" />

In an LTR locale (English), end = right. In an RTL locale (Arabic, Hebrew), end = left.

In ConstraintLayout

xml
1<androidx.constraintlayout.widget.ConstraintLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content">
4
5    <!-- Constrain to end of parent for right alignment -->
6    <TextView
7        android:layout_width="wrap_content"
8        android:layout_height="wrap_content"
9        android:text="Right aligned"
10        app:layout_constraintEnd_toEndOf="parent"
11        app:layout_constraintTop_toTopOf="parent" />
12
13    <!-- Full width with gravity -->
14    <TextView
15        android:layout_width="0dp"
16        android:layout_height="wrap_content"
17        android:gravity="end"
18        android:text="Right aligned text"
19        app:layout_constraintStart_toStartOf="parent"
20        app:layout_constraintEnd_toEndOf="parent"
21        app:layout_constraintTop_toTopOf="parent" />
22
23</androidx.constraintlayout.widget.ConstraintLayout>

Jetpack Compose

kotlin
1// Right-align text in Compose
2Text(
3    text = "Right aligned",
4    modifier = Modifier.fillMaxWidth(),
5    textAlign = TextAlign.End
6)
7
8// Inside a Row
9Row(
10    modifier = Modifier.fillMaxWidth(),
11    horizontalArrangement = Arrangement.End
12) {
13    Text("Right aligned")
14}
15
16// In a Column
17Column(
18    modifier = Modifier.fillMaxWidth(),
19    horizontalAlignment = Alignment.End
20) {
21    Text("Right aligned")
22}

Common Pitfalls

  • Using gravity on a wrap_content width TextView: If the TextView width is wrap_content, it is exactly as wide as the text. There is no extra space for alignment. Set width to match_parent or a fixed value for gravity to have a visible effect.
  • Confusing gravity with layout_gravity: gravity aligns content inside the view. layout_gravity positions the view inside its parent. Using layout_gravity="right" on a full-width TextView does nothing because the view already fills the parent.
  • Using right instead of end for RTL support: android:gravity="right" is hardcoded to the right side regardless of locale. For RTL languages (Arabic, Hebrew), use android:gravity="end" which flips automatically. Enable android:supportsRtl="true" in the manifest.
  • textAlignment overriding gravity: On API 17+, textAlignment takes precedence over gravity. If you set both to conflicting values, textAlignment wins. Either use one consistently or set textAlignment="gravity" to defer to the gravity attribute.
  • Right alignment in RecyclerView items: When a TextView inside a RecyclerView item has wrap_content width, right alignment requires setting the parent layout's gravity or constraints instead. Use ConstraintLayout with app:layout_constraintEnd_toEndOf="parent" on the TextView.

Summary

  • Use android:gravity="end" to right-align text content inside a TextView (RTL-aware)
  • Use textAlignment="viewEnd" for explicit text alignment (API 17+, overrides gravity)
  • The TextView width must be wider than the text for alignment to be visible — use match_parent
  • Prefer start/end over left/right for proper RTL language support
  • In Jetpack Compose, use textAlign = TextAlign.End with Modifier.fillMaxWidth()

Course illustration
Course illustration

All Rights Reserved.