Is it possible when creating a RelativeLayout at runtime to set the equivalent of android:layout_below
programmatically?
Asked
Active
Viewed 1.1e+01k times
217
4 Answers
484
Yes:
RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.below_id);
viewToLayout.setLayoutParams(params);
First, the code creates a new layout params by specifying the height and width. The addRule
method adds the equivalent of the xml properly android:layout_below
. Then you just call View#setLayoutParams
on the view you want to have those params.

Pankaj Lilan
- 4,245
- 1
- 29
- 48

Rich Schuler
- 41,814
- 6
- 72
- 59
-
13why can't this answer be accepted ? It's a very good explanation. – Adithya Feb 25 '13 at 18:05
-
Works for me. Used this approach to add a View ABOVE a view. This answer should be accepted right away. – Rohit Singh Oct 24 '21 at 04:27
154
Alternatively you can use the views current layout parameters and modify them:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewToLayout.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.below_id);

jackofallcode
- 1,935
- 1
- 13
- 15
-
14@EelLee It's pretty clearly another way to answer the question, which is why it has 11 upvotes. – Alex K Jan 04 '15 at 20:50
-
9This should have more upvotes than the other answer: to maintain consistent behavior of the view, we should work with existing `LayoutParams` rather than creating new ones. In fact, this should be accepted as the correct answer. – Kevin Kopf Oct 19 '16 at 21:27
13
While @jackofallcode answer is correct, it can be written in one line:
((RelativeLayout.LayoutParams) viewToLayout.getLayoutParams()).addRule(RelativeLayout.BELOW, R.id.below_id);

CoolMind
- 26,736
- 15
- 188
- 224
-
1this doesn't allow you to actually change the layout as you need a reference to the `LayoutParams` to set them with `setLayoutParams` – Stef Nov 18 '18 at 09:30
-
@Stef, I used this solution in one project. While jackofallcode solution is the only right here, I simplified it. But now I think shortening to one line is not good, because if any exception occurs, it would be harder to understand what operator raised it. – CoolMind Nov 19 '18 at 06:54
-
1@CoolMind I don't really like shortening code to 1 line, it makes it harder to read, especially when casting is involved. Plus doing it my way allows you to make further changes :) – jackofallcode Aug 15 '19 at 09:38
-
@jackofallcode, agree with you! Currently I also write in several lines, because when crash happens, it is easier to understand in what line an error occured. – CoolMind Aug 15 '19 at 10:12
5
Kotlin version with infix function
infix fun View.below(view: View) {
(this.layoutParams as? RelativeLayout.LayoutParams)?.addRule(RelativeLayout.BELOW, view.id)
}
Then you can write:
view1 below view2
Or you can call it as a normal function:
view1.below(view2)

Mahmoud
- 2,683
- 1
- 30
- 32