Set margins in a LinearLayout programmatically

Here is a little code to accomplish it: LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(30, 20, 30, 0); Button okButton=new Button(this); okButton.setText(“some text”); ll.addView(okButton, layoutParams);

Margin-Top not working for span element?

Unlike div, p 1 which are Block Level elements which can take up margin on all sides,span2 cannot as it’s an Inline element which takes up margins horizontally only. From the specification: Margin properties specify the width of the margin area of a box. The ‘margin’ shorthand property sets the margin for all four sides … Read more

CSS Margin Collapsing

No. When you have two adjacent vertical margins, the greater of the two is used and the other is ignored. So, for instance, if you have two block-display elements, A, followed by B beneath it, and A has a bottom-margin of 3em, while B has a top-margin of 2em, then the distance between them will … Read more

Margin on child element moves parent element

Found an alternative at Child elements with margins within DIVs You can also add: .parent { overflow: auto; } or: .parent { overflow: hidden; } This prevents the margins to collapse. Border and padding do the same. Hence, you can also use the following to prevent a top-margin collapse: .parent { padding-top: 1px; margin-top: -1px; … Read more

How to disable margin-collapsing?

There are two main types of margin collapse: Collapsing margins between adjacent elements Collapsing margins between parent and child elements Using a padding or border will prevent collapse only in the latter case. Also, any value of overflow different from its default (visible) applied to the parent will prevent collapse. Thus, both overflow: auto and … Read more

How wide is the default “ margin?

In most major browsers, the default margin is 8px on all sides. It is defined in pixels by the user-agent-stylesheet your browser provides. Some browsers allow you to create and use your own user-agent-stylesheet, but if you are developing a website, I would recommend staying away from changing this, since your users most likely will … Read more