How to right align widget in horizontal linear layout Android?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Right-aligning a widget inside a horizontal LinearLayout depends on what else is in the row. If there is only one child, parent gravity may be enough. If there are multiple children and you want one pushed to the far end, layout_weight is usually the correct tool.
Understand the Difference Between gravity and layout_gravity
These two attributes solve different problems:
- '
android:gravitycontrols how content sits inside a view or how children are placed inside certain parents' - '
android:layout_gravitycontrols how the child view itself is positioned within extra space provided by the parent'
In a horizontal LinearLayout, people often try layout_gravity="end" on the child and wonder why nothing changes. The reason is simple: if the parent is only as wide as its children need, there is no extra horizontal space for gravity to work with.
Single Child: Use Parent Gravity
If the row contains only one widget and the parent width is match_parent, parent gravity is enough:
This works because the parent has the full row width and can place its child at the end.
Multiple Children: Use Weight to Push the Right Widget
When the row has content on both sides, a weight-based spacer is the common solution:
The spacer consumes the flexible space in the middle, which pushes the button to the right edge.
Another Common Pattern: Weight on the Left View
If the left widget can expand, you can put the weight there instead of adding a spacer:
This is often cleaner than inserting an empty spacer view.
Prefer start and end Over left and right
For modern Android layouts, use start and end semantics where possible so right-to-left layout direction is handled more naturally. Hardcoding left and right can create localization issues later.
When layout_gravity Actually Helps
layout_gravity is not useless. It is just often misapplied. It can help when:
- the parent has extra space
- the child is smaller than the slot it has been given
- you are aligning vertically inside a horizontal row
For example, a shorter child can be vertically centered with layout_gravity="center_vertical" even in a horizontal LinearLayout. The problem is that developers often expect it to create horizontal space that only weight or parent width can provide.
Consider ConstraintLayout for More Complex Rows
If the row contains several text blocks, icons, badges, and trailing actions, LinearLayout plus spacer tricks can become brittle. In those cases, ConstraintLayout often expresses the intent more directly:
- constrain one view to
start - constrain another to
end - let the middle content flex safely
For simple rows, LinearLayout is still fine. The point is to stop fighting the layout system once the row becomes more than a basic left-plus-right arrangement.
Common Pitfalls
- Using
layout_gravity="right"on a child when the parent has no extra width to distribute. - Forgetting
layout_width="match_parent"on the parent and expecting end alignment to work anyway. - Using empty spacer views everywhere when a weighted content view would be simpler.
- Hardcoding
leftandrightinstead of thinking aboutstartandend. - Choosing
LinearLayoutfor a layout that has become complex enough to deserveConstraintLayout.
Summary
- Parent gravity works for a single child when the horizontal
LinearLayoutfills the row. - For multiple children, weight is usually what pushes the trailing widget to the right.
- A spacer view or a weighted left view are both valid approaches.
- '
layout_gravityonly helps when the parent actually has extra space to offer.' - Use
startandendsemantics for better layout-direction support. - Switch to
ConstraintLayoutwhen the row stops being simple enough for linear distribution.

