Right align text in android TextView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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 right — end adapts automatically based on the layout direction.
XML: gravity Attribute
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()
gravity vs layout_gravity
These two attributes serve different purposes:
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:
Available values:
textAlignment takes priority over gravity when both are set (API 17+).
Right-to-Left (RTL) Support
In an LTR locale (English), end = right. In an RTL locale (Arabic, Hebrew), end = left.
In ConstraintLayout
Jetpack Compose
Common Pitfalls
- Using
gravityon awrap_contentwidth TextView: If theTextViewwidth iswrap_content, it is exactly as wide as the text. There is no extra space for alignment. Set width tomatch_parentor a fixed value forgravityto have a visible effect. - Confusing
gravitywithlayout_gravity:gravityaligns content inside the view.layout_gravitypositions the view inside its parent. Usinglayout_gravity="right"on a full-widthTextViewdoes nothing because the view already fills the parent. - Using
rightinstead ofendfor RTL support:android:gravity="right"is hardcoded to the right side regardless of locale. For RTL languages (Arabic, Hebrew), useandroid:gravity="end"which flips automatically. Enableandroid:supportsRtl="true"in the manifest. textAlignmentoverridinggravity: On API 17+,textAlignmenttakes precedence overgravity. If you set both to conflicting values,textAlignmentwins. Either use one consistently or settextAlignment="gravity"to defer to thegravityattribute.- Right alignment in RecyclerView items: When a
TextViewinside a RecyclerView item haswrap_contentwidth, right alignment requires setting the parent layout's gravity or constraints instead. UseConstraintLayoutwithapp:layout_constraintEnd_toEndOf="parent"on theTextView.
Summary
- Use
android:gravity="end"to right-align text content inside aTextView(RTL-aware) - Use
textAlignment="viewEnd"for explicit text alignment (API 17+, overrides gravity) - The
TextViewwidth must be wider than the text for alignment to be visible — usematch_parent - Prefer
start/endoverleft/rightfor proper RTL language support - In Jetpack Compose, use
textAlign = TextAlign.EndwithModifier.fillMaxWidth()
Related reading
- Ripple effect on Android Lollipop CardView
- Room - Schema export directory is not provided to the annotation processor so we cannot export the schema
- Room - Schema export directory is not provided to the annotation processor so we cannot export the schema
- Room persistance library. Delete all
- RootViewController Switch Transition Animation
- Rotate Array in Swift
- Rotation methods deprecated, equivalent of ''didRotateFromInterfaceOrientation''?
- Round corner for BottomSheetDialogFragment
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.