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

Leave a Comment