3

I want to do something like this programmatically -

<RelativeLayout1>
  <LinearLayout name = 1>
  <LinearLayout below = 1>
<RelativeLayout1>

I try to do this:

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)mButtonContainer.getLayoutParams();

params have weight field, width field, but does not have layout_below... Thanks for any help!

ADK
  • 513
  • 3
  • 8
  • 22
  • This will help: (http://stackoverflow.com/questions/3277196/can-i-set-androidlayout-below-at-runtime-programmatically) – Ken Wolf Jun 07 '13 at 13:45
  • possible duplicate of [Add view programmatically to RelativeLayout](http://stackoverflow.com/questions/10418929/add-view-programmatically-to-relativelayout) – Pankaj Kumar Jun 07 '13 at 13:48

1 Answers1

12

with addRule

RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • And i could apply it to the LinearLayout? – ADK Jun 07 '13 at 13:47
  • 2
    @ADK: You get the `LinearLayout`'s current `RelativeLayout.LayoutParams`, then modify them. Or, if this is a newly-created `LinearLayout`, you create a new `RelativeLayout.LayoutParams` as the answer shows. The class of the `LayoutParams` is determined by the *parent* (`RelativeLayout`), not the *child* (`LinearLayout`). – CommonsWare Jun 07 '13 at 13:51
  • 1
    I try to do this: RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) mContainerView.getLayoutParams(); p.addRule(RelativeLayout.BELOW, R.id.animView); mButtonContainer.setLayoutParams(p); , where mContainerView - parent RelativeLayout, and mButtonContainer child LinearLayout....i`ve got an ClassCastException error =( – ADK Jun 07 '13 at 14:01