title: Add margin programmatically to RelativeLayout
body: <p>I'm trying to add programmatically a margin top to my <code>RelativeLayout</code> in my activity. Using the xml I can do it in this mode: <code>android:layout_marginTop="10dp"</code>, but when I'm trying to do it programmatically nothing changes... As you can see I'm using some <code>RelativeLayout</code> (there is a for loop) in one <code>LinearLayout</code> container.</p>
  
  <p>This is the code that I'm using:</p>
  
  <pre><code>//LINEAR LAYOUT
  LinearLayout linearLayout = new LinearLayout(this);
  linearLayout.setOrientation(LinearLayout.VERTICAL);
  linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
          LinearLayout.LayoutParams.WRAP_CONTENT));
  
  for (int i=1; i&lt;=3; i++){
      //RELATIVE LAYOUT
      RelativeLayout relativeLayout = new RelativeLayout(this);
      relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
              RelativeLayout.LayoutParams.FILL_PARENT));
      relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));
  
      //CODE FOR ADD MARGINS
      RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
      relativeParams.topMargin=80;
      relativeLayout.setLayoutParams(relativeParams);
  
      //IMAGE VIEW
      ImageView selectedPhoto = new ImageView(this);
      selectedPhoto.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
      selectedPhoto.setImageResource(R.drawable.ic_launcher);
  
      //TEXT VIEWS
      TextView numberCopies = new TextView(this);
      numberCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
      numberCopies.setGravity(Gravity.CENTER);
      numberCopies.setPadding(25, 25, 25, 25);
      numberCopies.setTextColor(getResources().getColor(R.color.blackColor));
      numberCopies.setText("2 copies ");
      RelativeLayout.LayoutParams layoutParamsNumberCopies = (RelativeLayout.LayoutParams) numberCopies.getLayoutParams();
      layoutParamsNumberCopies.addRule(RelativeLayout.CENTER_HORIZONTAL);
      numberCopies.setLayoutParams(layoutParamsNumberCopies);
  
      TextView priceCopies = new TextView(this);
      priceCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
      priceCopies.setGravity(Gravity.CENTER);
      numberCopies.setPadding(25, 25, 25, 25);
      priceCopies.setTextColor(getResources().getColor(R.color.redColor));
      priceCopies.setText("$ 25 ");
      RelativeLayout.LayoutParams layoutParamsPriceCopies = (RelativeLayout.LayoutParams) priceCopies.getLayoutParams();
      layoutParamsPriceCopies.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
      priceCopies.setLayoutParams(layoutParamsPriceCopies);
  
      relativeLayout.addView(selectedPhoto);
      relativeLayout.addView(numberCopies);
      relativeLayout.addView(priceCopies);
      linearLayout.addView(relativeLayout);
  }
  scrollView.addView(linearLayout);
  setContentView(scrollView);
  </code></pre>
  
  <p>I think that the failing block code is this:</p>
  
  <pre><code>//CODE FOR ADD MARGINS
  RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
  relativeParams.topMargin=80;
  relativeLayout.setLayoutParams(relativeParams);
  </code></pre>