Can I Set androidlayout_below at Runtime Programmatically?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Yes, you can reproduce android:layout_below at runtime, but you do it through RelativeLayout.LayoutParams rather than by editing XML attributes directly. The key idea is to update the target view’s layout rules in code and make sure both the anchor view and the moving view have valid IDs.
layout_below in XML Maps to addRule in Code
In XML, a RelativeLayout rule might look like this:
At runtime, the equivalent is:
That means the programmatic solution has three moving parts:
- the parent must be a
RelativeLayout - the anchor view must have an ID
- the target view must use
RelativeLayout.LayoutParams
If any of those are missing, the rule will not behave the way you expect.
A Complete Runtime Example
Here is a small Java example that places one TextView below another in code:
This is the direct runtime equivalent of defining the relationship in XML.
If the view already exists in your layout, you usually retrieve and modify its existing layout params instead of creating new views from scratch.
Updating an Existing View at Runtime
If both views are already in the layout, the pattern is shorter:
If you need to move the view again later, clear or replace conflicting rules before applying the new one. Otherwise, the view may keep old positioning constraints that fight with the new rule.
For example, on newer Android APIs:
That makes the layout change explicit and avoids stale constraints.
IDs Matter More Than People Expect
The most common reason this fails is missing IDs. XML views usually have them already, but dynamically created views do not unless you assign one.
Without a valid ID on the anchor view, RelativeLayout.BELOW has nothing reliable to reference.
Also note that layout_below is specifically a RelativeLayout concept. If your parent is LinearLayout, FrameLayout, or ConstraintLayout, the same code will not apply. In modern Android, ConstraintLayout is often preferred for new screens, and it has its own separate programmatic constraint APIs.
Common Pitfalls
The biggest mistake is trying to use RelativeLayout rules on a view whose parent is not a RelativeLayout. The layout params must match the parent layout type.
Another issue is forgetting to assign an ID to a dynamically created anchor view. Without that ID, the BELOW rule cannot target anything meaningful.
Developers also sometimes modify layout params and forget to set them back on the view. If the updated params are never reapplied, nothing changes on screen.
Finally, if the UI design is heavily dynamic, consider whether ConstraintLayout or a different layout structure would be easier to maintain than piling more RelativeLayout rules into runtime code.
It is also worth testing the result on different screen sizes after the rule change. A programmatic layout update that looks correct on one device can still expose hidden spacing or overlap assumptions elsewhere in the view hierarchy.
Summary
- Yes,
android:layout_belowcan be set programmatically. - The runtime equivalent is
RelativeLayout.LayoutParams.addRule(RelativeLayout.BELOW, anchorId). - Both the anchor and the moving view need valid IDs.
- The parent layout must actually be a
RelativeLayout. - If views are repositioned repeatedly, clear conflicting rules before applying new ones.

