Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add margin programmatically to RelativeLayout

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);
like image 838
Hieicker Avatar asked Sep 17 '13 08:09

Hieicker


People also ask

How do you set margin top programmatically?

You should use LayoutParams to set your button margins: LayoutParams params = new LayoutParams( LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT ); params.

How do I set margins to recyclerView programmatically?

margin); int marginTopPx = (int) (marginTopDp * getResources(). getDisplayMetrics(). density + 0.5f); layoutParams. setMargins(0, marginTopPx, 0, 0); recyclerView.


4 Answers

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();
like image 168
Chintan Rathod Avatar answered Oct 04 '22 18:10

Chintan Rathod


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..

like image 26
CRUSADER Avatar answered Oct 04 '22 18:10

CRUSADER


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)

like image 35
Guian Avatar answered Oct 04 '22 19:10

Guian


// kotlin

val relativeParams = relativeLayoutGeneral.layoutParams as FrameLayout.LayoutParams                   
relativeParams.setMargins(0, 7, 0, 111)
relativeLayoutGeneral.layoutParams = relativeParams
like image 29
Diseño Web Cantabria Avatar answered Oct 04 '22 18:10

Diseño Web Cantabria