Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 893
stealthjong Avatar asked Oct 04 '12 13:10

stealthjong


People also ask

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.

How do you set margins in Java?

We can set a margin to a JButton by using the setMargin() method of JButton class and pass Insets(int top, int left, int bottom, int right) as an argument.


1 Answers

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() ); 
like image 156
throrin19 Avatar answered Sep 23 '22 02:09

throrin19