How do I center text horizontally and vertically in a TextView in Android?
To center text both horizontally and vertically in an Android TextView, you can use the gravity XML attribute or the setGravity() method programmatically. The most straightforward approach is setting android:gravity="center" in your XML layout, which centers text within the TextView’s boundaries.
Contents
- Using XML Attributes
- Programmatic Approach
- Advanced Layout Methods
- Best Practices and Examples
- Troubleshooting Common Issues
Using XML Attributes
The simplest way to center text in a TextView is through XML attributes. There are two primary attributes you can use:
Basic Centering with Gravity
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered Text"
android:gravity="center" />
The gravity attribute controls how the content (text) is positioned within the TextView’s boundaries. Setting it to "center" centers both horizontally and vertically.
Alternative Values
You can also use specific gravity values:
<!-- Center horizontally only -->
android:gravity="center_horizontal"
<!-- Center vertically only -->
android:gravity="center_vertical"
<!-- Center in all directions -->
android:gravity="center"
Gravity vs Layout Gravity
It’s important to understand the difference between gravity and layout_gravity:
gravity- positions the TextView’s content within the TextViewlayout_gravity- positions the TextView itself within its parent layout
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:text="Centered Text"
android:gravity="center" />
Programmatic Approach
You can also center text programmatically using Java or Kotlin:
Java
TextView textView = findViewById(R.id.myTextView);
textView.setGravity(Gravity.CENTER);
Kotlin
val textView: TextView = findViewById(R.id.myTextView)
textView.gravity = Gravity.CENTER
Specific Gravity Options
// Center both horizontally and vertically
textView.setGravity(Gravity.CENTER);
// Center only horizontally
textView.setGravity(Gravity.CENTER_HORIZONTAL);
// Center only vertically
textView.setGravity(Gravity.CENTER_VERTICAL);
Advanced Layout Methods
Using ConstraintLayout
For more complex layouts, ConstraintLayout provides excellent control over positioning:
<TextView
android:id="@+id/centeredTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered Text"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Using RelativeLayout
RelativeLayout also allows centering within parent:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered Text"
android:gravity="center"
android:layout_centerInParent="true" />
Best Practices and Examples
Example with Background and Padding
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="Centered Text"
android:gravity="center"
android:background="#E0E0E0"
android:padding="16dp"
android:textSize="18sp"
android:textColor="#333333" />
Multiple Lines with Centering
For multi-line text, you might want additional attributes:
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="This is a longer text that will wrap to multiple lines and still be centered both horizontally and vertically"
android:gravity="center"
android:textAlignment="center"
android:maxLines="3"
android:ellipsize="end" />
Programmatically with Dynamic Content
TextView textView = findViewById(R.id.dynamicTextView);
textView.setText("Dynamic Centered Text");
textView.setGravity(Gravity.CENTER);
textView.setTextSize(16);
textView.setTextColor(Color.BLACK);
Troubleshooting Common Issues
Text Not Centered Despite Setting Gravity
If text doesn’t center, check these common issues:
- Layout Width/Height: Ensure TextView has proper dimensions
- Parent Constraints: Verify parent layout constraints
- Text Content: Empty strings won’t display
<!-- Fix: Check if TextView has proper dimensions -->
<TextView
android:layout_width="match_parent" <!-- Or specific width -->
android:layout_height="wrap_content" <!-- Or specific height -->
android:text="Text"
android:gravity="center" />
Centering in RecyclerView Items
<!-- In RecyclerView item layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered in RecyclerView"
android:gravity="center" />
</LinearLayout>
Centering in Dialogs
<!-- Dialog layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:padding="24dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dialog Center Text"
android:gravity="center"
android:textSize="18sp" />
</LinearLayout>
Conclusion
Centering text in Android TextViews is straightforward using the gravity attribute or setGravity() method. The key takeaways are:
- Use
android:gravity="center"in XML for basic centering - Understand the difference between
gravity(content positioning) andlayout_gravity(widget positioning) - Combine with other attributes like
layout_centerInParentin RelativeLayout for complex layouts - Consider your layout container - different containers (LinearLayout, ConstraintLayout, RelativeLayout) offer different centering approaches
- Test with different screen sizes to ensure proper centering across all devices
For most cases, setting android:gravity="center" is sufficient for horizontal and vertical text centering. For more advanced positioning, explore ConstraintLayout which provides the most flexibility for modern Android layouts.