Android Development
layout_gravity
Programmatically Set
UI Design
Android Layouts

How to set layout_gravity programmatically?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Android, layout_gravity belongs to a view's layout parameters, not to the view content itself. That is why setting it programmatically only works when you update the correct LayoutParams subclass for the parent container and then apply those parameters back to the child view.

Understand gravity Versus layout_gravity

Developers often mix up two different concepts:

  • 'gravity controls how content is positioned inside the view'
  • 'layout_gravity controls how the view itself is positioned inside its parent'

For example, a TextView can center its text with gravity, while the TextView as a whole is aligned to the end of a LinearLayout with layout_gravity.

Set layout_gravity on the Correct LayoutParams

If the parent is a LinearLayout, use LinearLayout.LayoutParams. Change the gravity field on those layout params, then assign them back to the child.

java
1LinearLayout parent = findViewById(R.id.parent_layout);
2TextView label = findViewById(R.id.status_label);
3
4LinearLayout.LayoutParams params =
5        (LinearLayout.LayoutParams) label.getLayoutParams();
6params.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
7label.setLayoutParams(params);

This works because LinearLayout understands the gravity field in its child layout params. If the child currently has no layout params because it was created in code and not yet attached, create them explicitly.

java
1TextView label = new TextView(this);
2LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
3        LinearLayout.LayoutParams.WRAP_CONTENT,
4        LinearLayout.LayoutParams.WRAP_CONTENT
5);
6params.gravity = Gravity.CENTER_HORIZONTAL;
7label.setLayoutParams(params);
8parent.addView(label);

Parent Layout Type Changes the API

layout_gravity is not universal. It only exists where the parent layout supports it. A few common cases:

  • 'LinearLayout uses LinearLayout.LayoutParams.gravity'
  • 'FrameLayout uses FrameLayout.LayoutParams.gravity'
  • 'CoordinatorLayout and ConstraintLayout have their own positioning models'

For a FrameLayout, the code is similar but the layout params type changes.

java
1ImageView avatar = findViewById(R.id.avatar);
2FrameLayout.LayoutParams params =
3        (FrameLayout.LayoutParams) avatar.getLayoutParams();
4params.gravity = Gravity.TOP | Gravity.END;
5avatar.setLayoutParams(params);

If you cast to the wrong type, you will get a ClassCastException. The parent, not the child, determines which LayoutParams subclass you must use.

Use requestLayout Only When Needed

Calling setLayoutParams usually triggers a layout pass automatically. If you mutate an existing params object in unusual code paths and the screen does not update immediately, requestLayout() can force a new layout pass.

java
params.gravity = Gravity.CENTER;
label.setLayoutParams(params);
label.requestLayout();

Do not scatter requestLayout() everywhere by default, though. Unnecessary layout passes can hurt scrolling and animations.

Dynamic Alignment Example

A realistic use case is aligning a status badge based on business state.

java
1private void alignBadge(View badge, boolean isCompact) {
2    LinearLayout.LayoutParams params =
3            (LinearLayout.LayoutParams) badge.getLayoutParams();
4
5    if (isCompact) {
6        params.gravity = Gravity.CENTER_HORIZONTAL;
7    } else {
8        params.gravity = Gravity.END;
9    }
10
11    badge.setLayoutParams(params);
12}

This keeps the layout logic in code when it depends on runtime state rather than a fixed XML definition.

Prefer XML for Static Layouts

Programmatic layout_gravity is useful for dynamic UI behavior, but XML is still better for static alignment because it is easier to inspect and cheaper to maintain. Put alignment in code only when it truly depends on runtime conditions such as data state, feature flags, or device mode.

Common Pitfalls

The most common mistake is setting view.setGravity(...) and expecting the parent alignment to change. That only affects the view's internal content.

Another mistake is using the wrong layout params class. If the parent is FrameLayout, LinearLayout.LayoutParams is wrong even if the child is a TextView.

Developers also sometimes forget to reapply the updated layout params with setLayoutParams. Mutating a local object without assigning it back can make it look like gravity changes are being ignored.

Finally, do not expect layout_gravity to work in layouts that do not support it. ConstraintLayout uses constraints, not gravity-based child placement.

Summary

  • 'layout_gravity belongs to the child view's layout params.'
  • Use the LayoutParams subclass that matches the parent layout.
  • Change the gravity field on those params and reapply them with setLayoutParams.
  • Use programmatic alignment only when layout decisions depend on runtime state.
  • If the parent layout does not support layout_gravity, use that layout's native positioning system instead.

Course illustration
Course illustration

All Rights Reserved.