23

I want to know how to set layout_marginLeft, layout_marginTop, layout_marginBottom programmatically using screen height and width. Please help me.

Thanks Monali

AndyN
  • 1,742
  • 1
  • 15
  • 30
Monali
  • 1,966
  • 12
  • 39
  • 59

1 Answers1

47

Here is an example (adapted from this answer):

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

params.setMargins(10, 20, 30, 40);

Button button = new Button(this);
button.setText("some text");
layout.addView(button, params);
Community
  • 1
  • 1
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200