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

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.