22

I'm trying to add programmatically a margin top to my RelativeLayout in my activity. Using the xml I can do it in this mode: android:layout_marginTop="10dp", but when I'm trying to do it programmatically nothing changes... As you can see I'm using some RelativeLayout (there is a for loop) in one LinearLayout container.

This is the code that I'm using:

//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<=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);

I think that the failing block code is this:

//CODE FOR ADD MARGINS
RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.topMargin=80;
relativeLayout.setLayoutParams(relativeParams);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Hieicker
  • 595
  • 1
  • 11
  • 29

4 Answers4

63

Got solution.

When you are using RelativeLayout inside LinearLayout, you need to use LinearLayout.LayoutParams instead of RelativeLayout.LayoutParams.

So replace your following code...

RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.setMargins(0, 80, 0, 0);  // left, top, right, bottom
relativeLayout.setLayoutParams(relativeParams);

with...

// CODE FOR ADD MARGINS
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
        new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
linearParams.setMargins(0, 80, 0, 0);
relativeLayout.setLayoutParams(linearParams);
relativeLayout.requestLayout();
jbyun94
  • 25
  • 6
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • 1
    it works perfectly thanks! You help me a lot! But why in this case you change RelativeLayout with LinearLayout? Wasn't the set margins related to the relativeLayout in my starter code? – Hieicker Sep 17 '13 at 08:40
  • 3
    [Plz refer here](http://stackoverflow.com/a/17563292/2345913)... BTW @ChintanRathod **NICE CATCH** – CRUSADER Sep 17 '13 at 08:42
  • 1
    @CRUSADER, you have already answered the question.. Thanks for appreciation.. ;) – Chintan Rathod Sep 17 '13 at 08:45
  • @ChintanRathod It works like a charm but the margin doesn't appply when I use this on Kitkat 4.4.2 API 19 Samsung S4 I don't understand why – Christophe Chenel Jan 15 '19 at 18:40
11

You can use setMargins (int left, int top, int right, int bottom).

Try like this..

//CODE FOR ADD MARGINS
    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    relativeParams.setMargins(0, 80, 0, 0);
    relativeLayout.setLayoutParams(relativeParams);

Layout params should be set with resp to parentLayout .. in this case its LinearLayout as rightly pointed by @ChintanRathod, so

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

will do the trick..

Community
  • 1
  • 1
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
  • It doesn't work. I need to add a top margin for each relativeLayout created. – Hieicker Sep 17 '13 at 08:12
  • Nope, the layout not change. Maybe i'm doing something wrong when i add the views or when i declare the first RelativeLayout? – Hieicker Sep 17 '13 at 08:25
4

Be aware that you are using a relative layout inside a linear layout so you should use LinearLayout.LayoutParams

I would replace this :

        //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);

by this :

        //RELATIVE LAYOUT WITH PROPER LAYOUT PARAMS TO ADD MARGINS
        RelativeLayout relativeLayout = new RelativeLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 80, 0, 0);
        relativeLayout.setLayoutParams(params);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));

Note also that you should use WRAP_CONTENT as height configuration for children in a vertical linearLayout. ( in FILL_PARENT or MATCH_PARENT mode it will fill the parent without leaving space for the other children)

Guian
  • 4,563
  • 4
  • 34
  • 54
3

// kotlin

val relativeParams = relativeLayoutGeneral.layoutParams as FrameLayout.LayoutParams                   
relativeParams.setMargins(0, 7, 0, 111)
relativeLayoutGeneral.layoutParams = relativeParams
David Buck
  • 3,752
  • 35
  • 31
  • 35