Skip to content

Instantly share code, notes, and snippets.

@dynoChris
Last active January 20, 2019 15:05
Show Gist options
  • Save dynoChris/a06b47aeba4478da67754f1af22561eb to your computer and use it in GitHub Desktop.
Save dynoChris/a06b47aeba4478da67754f1af22561eb to your computer and use it in GitHub Desktop.
How to set margin programmatically in Android?
public static void setMargins (View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(left, top, right, bottom);
view.requestLayout();
}
}
//How to call it — Utils.setMargin(imageView, 0, 20, 0, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment