align text center with android
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
This centers the text inside the TextView rectangle.
layout_gravity does something different. It asks the parent layout to position the whole view.
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.
That centers horizontally and vertically within the view.
You can also be more specific:
Use those when only one axis should be centered.
Centering A View In Its Parent
In a LinearLayout, layout_gravity is the usual tool.
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.
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.
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.
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.
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.
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
gravityto center text inside aTextView. - Use
layout_gravityor 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. - '
gravityandtextAlignmentsolve related but different alignment problems.'
Related reading
- Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets
- Aligning UIToolBar items
- All com.android.support libraries must use the exact same version specification
- Allow multi-line in EditText view in Android?
- Allow only Numbers for UITextField input
- Allow user to select camera or gallery for image
- Allowing interaction with a UIView under another UIView
- Always pass weak reference of self into block in ARC?
.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.