views:

151

answers:

1

Is it possible to set in Java code (when creating a relative layout at runtime) the equivalent of android:layout_below ?

+3  A: 

Yes:

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);

p.addRule(RelativeLayout.BELOW, R.id.below_id);

viewToLayout.setLayoutParams(p);

First the code creates a new layout params by specifying the height and width. The addRule method adds the equivalent of the xml propery android:layout_below. Then you just call View#setLayoutParams on the view you want to have those params.

Qberticus
Qberticus - thanks very much for the detailed and clear reply. I will check this out.
AlanH
This works fine for me. Thanks again.
AlanH