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:
- '
gravitycontrols how content is positioned inside the view' - '
layout_gravitycontrols 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.
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.
Parent Layout Type Changes the API
layout_gravity is not universal. It only exists where the parent layout supports it. A few common cases:
- '
LinearLayoutusesLinearLayout.LayoutParams.gravity' - '
FrameLayoutusesFrameLayout.LayoutParams.gravity' - '
CoordinatorLayoutandConstraintLayouthave their own positioning models'
For a FrameLayout, the code is similar but the layout params type changes.
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.
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.
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_gravitybelongs to the child view's layout params.' - Use the
LayoutParamssubclass that matches the parent layout. - Change the
gravityfield on those params and reapply them withsetLayoutParams. - 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.

