In Android, how do I set margins in dp programmatically?
Asked Answered
P

31

542

In this, this and this thread I tried to find an answer on how to set the margins on a single view. However, I was wondering if there isn't an easier way. I'll explain why I rather wouldn't want to use this approach:

I have a custom Button which extends Button. If the background is set to something else than the default background (by calling either setBackgroundResource(int id) or setBackgroundDrawable(Drawable d)), I want the margins to be 0. If I call this:

public void setBackgroundToDefault() {
    backgroundIsDefault = true;
    super.setBackgroundResource(android.R.drawable.btn_default);
    // Set margins somehow
}

I want the margins to reset to -3dp (I already read here how to convert from pixels to dp, so once I know how to set margins in px, I can manage the conversion myself). But since this is called in the CustomButton class, the parent can vary from LinearLayout to TableLayout, and I'd rather not have him get his parent and check the instanceof that parent. That'll also be quite inperformant, I imagine.

Also, when calling (using LayoutParams) parentLayout.addView(myCustomButton, newParams), I don't know if this adds it to the correct position (haven't tried however), say the middle button of a row of five.

Question: Is there any easier way to set the margin of a single Button programmatically besides using LayoutParams?

EDIT: I know of the LayoutParams way, but I'd like a solution that avoids handling each different container type:

ViewGroup.LayoutParams p = this.getLayoutParams();
    if (p instanceof LinearLayout.LayoutParams) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)p;
        if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
        else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
        this.setLayoutParams(lp);
    }
    else if (p instanceof RelativeLayout.LayoutParams) {
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)p;
        if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
        else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
        this.setLayoutParams(lp);
    }
    else if (p instanceof TableRow.LayoutParams) {
        TableRow.LayoutParams lp = (TableRow.LayoutParams)p;
        if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
        else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
        this.setLayoutParams(lp);
    }
}

Because this.getLayoutParams();returns a ViewGroup.LayoutParams, which do not have the attributes topMargin, bottomMargin, leftMargin, rightMargin. The mc instance you see is just a MarginContainer which contains offset (-3dp) margins and (oml, omr, omt, omb) and the original margins (ml, mr, mt, mb).

Petrous answered 4/10, 2012 at 13:22 Comment(0)
T
976

You should use LayoutParams to set your button margins:

LayoutParams params = new LayoutParams(
        LayoutParams.WRAP_CONTENT,      
        LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);

Depending on what layout you're using you should use RelativeLayout.LayoutParams or LinearLayout.LayoutParams.

And to convert your dp measure to pixel, try this:

Resources r = mContext.getResources();
int px = (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        yourdpmeasure, 
        r.getDisplayMetrics()
);
Tourniquet answered 4/10, 2012 at 13:36 Comment(12)
From which package should I import that particular LayoutParams?Petrous
note that your conversion is the wrong way, from dp -> px. I needed the other way around. But I already have a solution.Petrous
setMargins used px and you will use dp, my conversion is right : dp -> px to set correct margin value.Tourniquet
@ChristiaandeJong RelativeLayout.LayoutParamsGushy
@Gushy Why RelativeLayout.LayoutParams and not LinearLayout.LayoutParams?Gerlach
it depends of your current layout. If you are in LinearLayout, use LinearLayout.LayoutParams. RelativeLayout.LayoutParams otherwiseTourniquet
You can use your concrete type or use MarginLayoutParams as Lyusten suggested. It is the base class for all Layouts.Flexure
you should import layoutParams that is your parent layout ex. <linearlayout><relativelayout><gridlayout> and you are working with grid layout. then, you need to use relativelayout.layoutparamsDacoit
btw what is wrong with using (int) getResources().getDimension(R.dimen.my_dp); to find the dp valueInhaler
getDimension si to get the dimension of known Ressources defined in your resources folder, here it's to get the dimension of another resources not defiend in the resources folderTourniquet
What if I'm using FrameLayout as the parent layout? There's no setMargin in FrameLayout.LayoutParams.Skewbald
@Petrous , take its parent layout.Drab
C
294

LayoutParams - NOT WORKING ! ! !

Need use type of: MarginLayoutParams

MarginLayoutParams params = (MarginLayoutParams) vector8.getLayoutParams();
params.width = 200; params.leftMargin = 100; params.topMargin = 200;

Code Example for MarginLayoutParams:

http://www.codota.com/android/classes/android.view.ViewGroup.MarginLayoutParams

Condolence answered 23/3, 2014 at 22:32 Comment(5)
Correct, but no need to set it back, the changed params are automatically reflected. Thus you can remove the line: vector8.setLayoutParams(params);Lipscomb
LayaoutParams usually create confusion while setting margin... So this MarginLayoutParams is very useful. ThanksBinge
you DO need to setLayoutParams(params) after marging changesTherianthropic
i found MarginLayoutParams as new class today #Thanks.Drab
Doesn't work for Button view: ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) button.getLayoutParams() returns nullTerwilliger
P
156

Best way ever:

private void setMargins (View view, int left, int top, int right, int bottom) {
    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        p.setMargins(left, top, right, bottom);
        view.requestLayout();
    }
}

How to call method:

setMargins(mImageView, 50, 50, 50, 50);

Hope this will help you.

Purchase answered 4/2, 2016 at 11:34 Comment(2)
i am facing issue when i set setMargins(holder.vCenter, 0, 20, 0,0); like this its leave margin both sides (top and bottom) whats wrong with above params?Alpenhorn
why do we need requestLayout() ?Uriisa
C
50
int sizeInDP = 16;

int marginInDp = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, sizeInDP, getResources()
                    .getDisplayMetrics());

Then

layoutParams = myView.getLayoutParams()
layoutParams.setMargins(marginInDp, marginInDp, marginInDp, marginInDp);
myView.setLayoutParams(layoutParams);

Or

LayoutParams layoutParams = new LayoutParams...
layoutParams.setMargins(marginInDp, marginInDp, marginInDp, marginInDp);
myView.setLayoutParams(layoutParams);
Colubrine answered 16/8, 2015 at 19:49 Comment(1)
what is getResources()?Posturize
D
50

With Android KTX, you can do something like that:

yourView.updateLayoutParams<ViewGroup.MarginLayoutParams> {
   setMargins(0, 0, 0, 0)
}
Dither answered 1/7, 2021 at 8:46 Comment(0)
S
27

Here is the all-in-one answer with recent updates:

Step 1, to update margin

The basic idea is to get margin out and then update it. The update will be applies automatically and you do not need to set it back. To get the layout parameters, simply call this method:

LayoutParams layoutParams = (LayoutParams) yourView.findViewById(R.id.THE_ID).getLayoutParams();

The LayoutParams comes from the layout of your view. If the view is from a linear layout, you need to import LinearLayout.LayoutParams. If you use relative layout, import LinearLayout.LayoutParams , etc.

Now, if you set the margin using Layout_marginLeft, Right, etc, you need to update margin in this way

layoutParams.setMargins(left, top, right, bottom);

If you set margin using the new layout_marginStart, you need to update margin in this way

layoutParams.setMarginStart(start);
layoutParams.setMarginEnd(end);

Step 2, to update margin in dp

All two ways of updating margin above are updating in pixels. You need to do a translation of dp to pixels.

float dpRatio = context.getResources().getDisplayMetrics().density;
int pixelForDp = (int)dpValue * dpRatio;

Now put the calculated value to the above margin update functions and you should be all set

Soupspoon answered 29/11, 2017 at 17:38 Comment(0)
I
17

In Kotlin it will look like this:

val layoutParams = (yourView?.layoutParams as? MarginLayoutParams)
layoutParams?.setMargins(40, 40, 40, 40)
yourView?.layoutParams = layoutParams
Ineducable answered 23/9, 2019 at 12:33 Comment(1)
This doesn't answer the question as the setMargins method takes in only values in pixels and not dp, which is what the user is asking.Staffard
O
10

layout_margin is a constraint that a view child tell to its parent. However it is the parent's role to choose whether to allow margin or not. Basically by setting android:layout_margin="10dp", the child is pleading the parent view group to allocate space that is 10dp bigger than its actual size. (padding="10dp", on the other hand, means the child view will make its own content 10dp smaller.)

Consequently, not all ViewGroups respect margin. The most notorious example would be listview, where the margins of items are ignored. Before you call setMargin() to a LayoutParam, you should always make sure that the current view is living in a ViewGroup that supports margin (e.g. LinearLayouot or RelativeLayout), and cast the result of getLayoutParams() to the specific LayoutParams you want. (ViewGroup.LayoutParams does not even have setMargins() method!)

The function below should do the trick. However make sure you substitute RelativeLayout to the type of the parent view.

private void setMargin(int marginInPx) {
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
    lp.setMargins(marginInPx,marginInPx, marginInPx, marginInPx);
    setLayoutParams(lp);
}
Overnice answered 1/4, 2015 at 0:25 Comment(0)
C
10

This method will let you set the Margin in DP

public void setMargin(Context con,ViewGroup.LayoutParams params,int dp) {

        final float scale = con.getResources().getDisplayMetrics().density;
        // convert the DP into pixel
        int pixel =  (int)(dp * scale + 0.5f); 

        ViewGroup.MarginLayoutParams s =(ViewGroup.MarginLayoutParams)params;
        s.setMargins(pixel,pixel,pixel,pixel);

        yourView.setLayoutParams(params);
}

UPDATE

You can change the parameter that suits your need.

Closelipped answered 26/5, 2015 at 7:32 Comment(0)
K
10

With Kotlin you can use the View.updateLayoutParams extension function:

yourView.updateLayoutParams<ViewGroup.MarginLayoutParams> {
    setMargins(15,15,15,15)
}

// or

yourView.updateLayoutParams<ViewGroup.MarginLayoutParams> {
    topMargin = 15
}
Kwok answered 23/2, 2022 at 5:10 Comment(0)
D
8

You can use this method and put static dimen like 20 it converts according your device

 public static int dpToPx(int dp) 
      {
          float scale = context.getResources().getDisplayMetrics().density;
       return (int) (dp * scale + 0.5f);
  }
Daughterinlaw answered 29/5, 2019 at 7:47 Comment(0)
Q
8

Simple Kotlin Extension Solutions

Set all/any side independently:

fun View.setMargin(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
    val params = (layoutParams as? MarginLayoutParams)
    params?.setMargins(
            left ?: params.leftMargin,
            top ?: params.topMargin,
            right ?: params.rightMargin,
            bottom ?: params.bottomMargin)
    layoutParams = params
}

myView.setMargin(10, 5, 10, 5)
// or just any subset
myView.setMargin(right = 10, bottom = 5)

Directly refer to a resource values:

fun View.setMarginRes(@DimenRes left: Int? = null, @DimenRes top: Int? = null, @DimenRes right: Int? = null, @DimenRes bottom: Int? = null) {
    setMargin(
            if (left == null) null else resources.getDimensionPixelSize(left),
            if (top == null) null else resources.getDimensionPixelSize(top),
            if (right == null) null else resources.getDimensionPixelSize(right),
            if (bottom == null) null else resources.getDimensionPixelSize(bottom),
    )
}

myView.setMarginRes(top = R.dimen.my_margin_res)

To directly set all sides equally as a property:

var View.margin: Int
    get() = throw UnsupportedOperationException("No getter for property")
    set(@Px margin) = setMargin(margin, margin, margin, margin)
   
myView.margin = 10 // px

// or as res
var View.marginRes: Int
    get() = throw UnsupportedOperationException("No getter for property")
    set(@DimenRes marginRes) {
        margin = resources.getDimensionPixelSize(marginRes)
    }

myView.marginRes = R.dimen.my_margin_res

To directly set a specific side, you can create a property extension like this:

var View.leftMargin
    get() = marginLeft
    set(@Px leftMargin) = setMargin(left = leftMargin)

var View.leftMarginRes: Int
    get() = throw UnsupportedOperationException("No getter for property")
    set(@DimenRes leftMarginRes) {
        leftMargin = resources.getDimensionPixelSize(leftMarginRes)
    }

This allows you to make horizontal or vertical variants as well:

var View.horizontalMargin
    get() = throw UnsupportedOperationException("No getter for property")
    set(@Px horizontalMargin) = setMargin(left = horizontalMargin, right = horizontalMargin)

var View.horizontalMarginRes: Int
    get() = throw UnsupportedOperationException("No getter for property")
    set(@DimenRes horizontalMarginRes) {
        horizontalMargin = resources.getDimensionPixelSize(horizontalMarginRes)
    }

NOTE: If margin is failing to set, you may too soon before render, meaning params == null. Try wrapping the modification with myView.post{ margin = 10 }

Quinsy answered 30/8, 2020 at 4:59 Comment(0)
J
8

That how I have done in kotlin

fun View.setTopMargin(@DimenRes dimensionResId: Int) {
    (layoutParams as ViewGroup.MarginLayoutParams).topMargin = resources.getDimension(dimensionResId).toInt()
}
Joinery answered 24/9, 2020 at 23:1 Comment(0)
B
6

If you want to add a margin to your TextView you will have to LayoutParams:

val params =  LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)
params.setMargins(int left, int top, int right, int bottom)
your_view.layoutParams = params

LayoutParams can be any layouts like Relative, Linear, View or ViewGroups. Choose the LayoutParams as you need. Thanks

Ballyhoo answered 23/9, 2019 at 13:12 Comment(4)
op asked for marginsCryoscope
I mentioned both padding and margin. I think you misunderstood. I hope you read my whole answer properly! @anshsachdevaBallyhoo
yes, my bad. its now 2 days since i commented, so i can't change my -1. kindly make edits to your answer to highlight the actual solution and i will change it :)Cryoscope
@anshsachdeva, I have updated the answer of mine. Let me know if it's helpful or not. ThanksBallyhoo
A
4

Use this method to set margin in dp

private void setMargins (View view, int left, int top, int right, int bottom) {
    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();

        final float scale = getBaseContext().getResources().getDisplayMetrics().density;
        // convert the DP into pixel
        int l =  (int)(left * scale + 0.5f);
        int r =  (int)(right * scale + 0.5f);
        int t =  (int)(top * scale + 0.5f);
        int b =  (int)(bottom * scale + 0.5f);

        p.setMargins(l, t, r, b);
        view.requestLayout();
    }
}

call the method :

setMargins(linearLayout,5,0,5,0);
Ainslie answered 20/6, 2018 at 15:29 Comment(2)
Simply simple!. ThanksIse
Simply done. Worked Perfectly.Confession
O
3

When you are in a custom View, you can use getDimensionPixelSize(R.dimen.dimen_value), in my case, I added the margin in LayoutParams created on init method.

In Kotlin

init {
    LayoutInflater.from(context).inflate(R.layout.my_layout, this, true)
    layoutParams = LayoutParams(MATCH_PARENT, WRAP_CONTENT).apply {
    val margin = resources.getDimensionPixelSize(R.dimen.dimen_value)
    setMargins(0, margin, 0, margin)
}

in Java:

public class CustomView extends LinearLayout {

    //..other constructors

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        int margin = getResources().getDimensionPixelSize(R.dimen.spacing_dime);
        params.setMargins(0, margin, 0, margin);
        setLayoutParams(params);
    }
}
Outspoken answered 1/8, 2018 at 4:28 Comment(0)
M
2

Working utils function using DP for those interested:

public static void setMargins(Context context, View view, int left, int top, int right, int bottom) {
    int marginLeft = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, left, context.getResources().getDisplayMetrics());
    int marginTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, top, context.getResources().getDisplayMetrics());
    int marginRight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, right, context.getResources().getDisplayMetrics());
    int marginBottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, bottom, context.getResources().getDisplayMetrics());

    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        p.setMargins(marginLeft, marginTop, marginRight, marginBottom);
        view.requestLayout();
    }
}
Mead answered 12/1, 2021 at 15:24 Comment(0)
C
2

Based on other answers, i made a generic extension function, which identifies your parent and uses the params accordingly:

//takes margin values as integer , eg for 12dp top , you will pass 12
fun View?.setMarginFromConstant(mLeft:Int, mTop:Int, mRight:Int, mBottom:Int){
    this?.apply {
        val left = context?.dpToPixel(mLeft)?:0
        val top = context?.dpToPixel(mTop)?:0
        val right = context?.dpToPixel(mRight)?:0
        val bottom = context?.dpToPixel(mBottom)?:0
        when (val params = this.layoutParams) {
            is ConstraintLayout.LayoutParams -> {
                params.marginStart = left
                params.marginEnd = right
                params.topMargin = top
                params.bottomMargin = bottom
            }
            is FrameLayout.LayoutParams -> {
                params.marginStart = left
                params.marginEnd = right
                params.topMargin = top
                params.bottomMargin = bottom
            }
            is RecyclerView.LayoutParams -> {
                params.marginStart = left
                params.marginEnd = right
                params.topMargin = top
                params.bottomMargin = bottom
            }
        }
    }

}

and

fun Context.dpToPixel(dp: Int): Int =
    (dp * applicationContext.resources.displayMetrics.density).toInt()

You can add support for other parent view groups too

Cryoscope answered 11/3, 2021 at 8:48 Comment(0)
H
2
val params = FrameLayout.LayoutParams(
    ViewGroup.LayoutParams.MATCH_PARENT,
    ViewGroup.LayoutParams.MATCH_PARENT
)
params.setMargins(0, 0, 0, 400)
binding.container.setLayoutParams(params)
Headley answered 21/7, 2022 at 10:13 Comment(0)
A
2

The answer of @Lyusten Elder is correct, but don't forget that you need to convert px to dp before doing it.

Like this:

    public static int getPxFromDp(Context context, float dp) {
        return Math.round(dp * context.getResources().getDisplayMetrics().density);
    }

    public static int getDpFromPx(Context context, float px) {
        return Math.round(px / context.getResources().getDisplayMetrics().density);
    }
Antiphon answered 11/12, 2022 at 13:30 Comment(0)
T
1

For a quick one-line setup use

((LayoutParams) cvHolder.getLayoutParams()).setMargins(0, 0, 0, 0);

but be carfull for any wrong use to LayoutParams, as this will have no if statment instance chech

Thrombo answered 18/7, 2018 at 0:55 Comment(0)
X
1

Created a Kotlin Extension function for those of you who might find it handy.

Make sure to pass in pixels not dp. Happy coding :)

fun View.addLayoutMargins(left: Int? = null, top: Int? = null,
                      right: Int? = null, bottom: Int? = null) {
    this.layoutParams = ViewGroup.MarginLayoutParams(this.layoutParams)
            .apply {
                left?.let { leftMargin = it }
                top?.let { topMargin = it }
                right?.let { rightMargin = it }
                bottom?.let { bottomMargin = it }
            }
}
Xever answered 26/4, 2019 at 4:35 Comment(1)
android.view.ViewGroup$MarginLayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams. :(Audiology
S
1

In my example i am adding an ImageView to a LinearLayout programatically. I have set top and bottom margins to ImagerView. Then adding the ImageView to the LinearLayout.



        ImageView imageView = new ImageView(mContext);
        imageView.setImageBitmap(bitmap);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(0, 20, 0, 40);
        imageView.setLayoutParams(params);
        linearLayout.addView(imageView);

Sheave answered 9/5, 2019 at 10:26 Comment(0)
G
1

Use this method to you can set dp correctly:

public int dpFormat(int dp) {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

and then call

setMargins(dpFormat(15), dpFormat(15), dpFormat(15), dpFormat(15));
Ghyll answered 17/9, 2021 at 19:54 Comment(1)
its work for me but maybe there should be getApplicationContext() instead of getContext().Duckboard
B
1

This works for me. hope this helps. First, create a parameter with height and weight then set margins as per your need then set this parameter to the layout you need. The setMargins(0, 0, 0, 150) function has sequence margins of (left, top, right, and bottom) respectively.

            ScrollView svOfferDesc = findViewById(R.id.scroll_view);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
            );
            params.setMargins(0, 0, 0, 150);
            svOfferDesc.setLayoutParams(params);
Byssinosis answered 13/10, 2023 at 6:9 Comment(0)
V
0

As today, the best is probably to use Paris, a library provided by AirBnB.

Styles can then be applied like this:

Paris.style(myView).apply(R.style.MyStyle);

it also support custom view (if you extend a view) using annotations:

@Styleable and @Style
Vervain answered 15/5, 2018 at 0:39 Comment(0)
S
0

For me I was using ViewGroup.LayoutParams in a themedbutton component. So using ViewGroup.LayoutParams unable to use setMargin.

Instead use MarginLayoutParams as below, it worked for me.

ViewGroup.MarginLayoutParams params =new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
        params.setMargins(0,8,0,0);
Sarajane answered 7/7, 2022 at 11:56 Comment(0)
S
0

For me, the following code worked

buttonLinearLayout.layoutParams as MarginLayoutParams).apply 
{
   top.run { 
        topMargin = resources.getDimension(R.dimen.spacing).toInt() 
     }                        
}
Servomechanism answered 9/7, 2022 at 7:53 Comment(0)
S
0

first create dimens.xml file in values directory and create dimen value on it

<resources>
    <dimen name="circle_margin">5dp</dimen>
</resources>

then create layoutparams variable and apply on it.

 val layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
                layoutParams.setMargins(resources.
    getDimension(R.dimen.circle_margin).toInt())
                layoutParams.gravity = Gravity.CENTER
                binding.imageView.layoutParams = layoutParams
       
Saltsman answered 22/7, 2023 at 12:34 Comment(0)
E
-1
((FrameLayout.LayoutParams) linearLayout.getLayoutParams()).setMargins(450, 20, 0, 250);
        linearLayout.setBackgroundResource(R.drawable.smartlight_background);

I had to cast mine to FrameLayout for a linearLayout as it inherits from it and set margins there so the activity appears only on part of the screen along with a different background than the original layout params in the setContentView.

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
linearLayout.setBackgroundColor(getResources().getColor(R.color.full_white));
setContentView(linearLayout,layoutParams);

None of the others worked for using the same activity and changing the margins based on opening the activity from a different menu! setLayoutParams never worked for me - the device would crash every single time. Even though these are hardcoded numbers - this is only an example of the code for demonstration purposes only.

Emelina answered 15/2, 2019 at 13:21 Comment(0)
M
-1

You can use ViewGroup.MarginLayoutParams to set the width, height and margins

ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
marginLayoutParams.setMargins(0,16,0,16);
linearLayout.setLayoutParams(marginLayoutParams);

Where the method setMargins(); takes in values for left, top, right, bottom respectively. Clockwise!, starting from the left.

Mariellamarielle answered 3/6, 2019 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.