How to set margin of ImageView using code, not xml

I want to add an unknown number of ImageView views to my layout with margin. In XML, I can use layout_margin like this: <ImageView android:layout_margin=”5dip” android:src=”https://stackoverflow.com/questions/3416087/@drawable/image” /> There is ImageView.setPadding(), but no ImageView.setMargin(). I think it’s along the lines of ImageView.setLayoutParams(LayoutParams), but not sure what to feed into that. Does anyone know? 16 Answers 16

How to disable margin-collapsing?

Is there a way to disable margin-collapsing altogether? The only solutions I’ve found (by the name of “uncollapsing”) entail using a 1px border or 1px padding. I find this unacceptable: the extraneous pixel complicates calculations for no good reason. Is there a more reasonable way to disable this margin-collapsing? 12 Answers 12

Margin-Top not working for span element?

Can someone tell me what I coded wrong? Everything is working, the only thing is that there is no margin at the top. HTML: <div id=”contact_us”> <!– BEGIN CONTACT US –> <span class=”first_title”>Contact</span> <span class=”second_title”>Us</span> <p class=”content”>For any questions whatsoever please contact us through the following e-mail address:</p></br></br> <p class=”e-mail”>[email protected]</p></br></br></br></br> <p class=”read_more”><a href=”https://stackoverflow.com/questions/11700985/underconstruction.html”>Read More</a></p> </div> … Read more

Set margins in a LinearLayout programmatically

I’m trying to use Java (not XML) to create a LinearLayout with buttons that fill the screen, and have margins. Here is code that works without margins: LinearLayout buttonsView = new LinearLayout(this); buttonsView.setOrientation(LinearLayout.VERTICAL); for (int r = 0; r < 6; ++r) { Button btn = new Button(this); btn.setText(“A”); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); … Read more

Why is there an unexplainable gap between these inline-block div elements? [duplicate]

This question already has answers here: How to remove the space between inline/inline-block elements? (41 answers) Closed 2 years ago. I have two inline-block div elements, that are the same, positioned next to eachother. However there seems to be a mysterious space of 4 pixels between the two divs despite the margin being set to … Read more