Android
LinearLayout
Right Align
UI Design
Android Development

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:gravity controls how content sits inside a view or how children are placed inside certain parents'
  • 'android:layout_gravity controls 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:

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="horizontal"
5    android:gravity="end">
6
7    <Button
8        android:layout_width="wrap_content"
9        android:layout_height="wrap_content"
10        android:text="Action" />
11
12</LinearLayout>

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:

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="horizontal">
5
6    <TextView
7        android:layout_width="wrap_content"
8        android:layout_height="wrap_content"
9        android:text="Title" />
10
11    <View
12        android:layout_width="0dp"
13        android:layout_height="0dp"
14        android:layout_weight="1" />
15
16    <Button
17        android:layout_width="wrap_content"
18        android:layout_height="wrap_content"
19        android:text="Edit" />
20
21</LinearLayout>

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:

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="horizontal">
5
6    <TextView
7        android:layout_width="0dp"
8        android:layout_height="wrap_content"
9        android:layout_weight="1"
10        android:text="Title" />
11
12    <ImageButton
13        android:layout_width="wrap_content"
14        android:layout_height="wrap_content"
15        android:contentDescription="More"
16        android:src="@android:drawable/ic_menu_more" />
17
18</LinearLayout>

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 left and right instead of thinking about start and end.
  • Choosing LinearLayout for a layout that has become complex enough to deserve ConstraintLayout.

Summary

  • Parent gravity works for a single child when the horizontal LinearLayout fills 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_gravity only helps when the parent actually has extra space to offer.'
  • Use start and end semantics for better layout-direction support.
  • Switch to ConstraintLayout when the row stops being simple enough for linear distribution.

Course illustration
Course illustration

All Rights Reserved.