views:

3113

answers:

2

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); // Verbose!
   lp.weight = 1.0f; // This is critical. Doesn't work without it.
   buttonsView.addView(btn, lp);
  }
  ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
  setContentView(buttonsView, lp);

So that works fine, but how on earth do you give the buttons margins so there is space between them? I tried using LinearLayout.MarginLayoutParams, but that has no weight member so it's no good. And it doesn't work if you pass it lp in its constructor either.

Is this impossible? Because it sure looks it, and it wouldn't be the first Android layout task you can only do in XML.

+1  A: 

So that works fine, but how on earth do you give the buttons margins so there is space between them?

You call setMargins() on the LinearLayout.LayoutParams object.

I tried using LinearLayout.MarginLayoutParams, but that has no weight member so it's no good.

LinearLayout.LayoutParams is a subclass of LinearLayout.MarginLayoutParams, as indicated in the documentation.

Is this impossible?

No.

it wouldn't be the first Android layout task you can only do in XML

You are welcome to supply proof of this claim.

Personally, I am unaware of anything that can only be accomplished via XML and not through Java methods in the SDK. In fact, by definition, everything has to be doable via Java (though not necessarily via SDK-reachable methods), since XML is not executable code. But, if you're aware of something, point it out, because that's a bug in the SDK that should get fixed someday.

CommonsWare
Ha, ok I understand now. Was getting a bit lost in the layout system which is rather complicated. I don't think there's anyway to set layout_widgth/height at runtime.
Timmmm
And there's no way to set the style: http://stackoverflow.com/questions/2016249/how-to-programmatically-setting-style-attribute-in-a-view
Timmmm
I haven't had a problem with setting width and height at runtime via `LayoutPararms`. I terms of styles...yeah, well, there's a reason I don't use 'em. :-)
CommonsWare
+3  A: 

Here is a little code to accomplish it:

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

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

layoutParams.setMargins(30, 20, 30, 0);

Button okButton=new Button(this);
okButton.setText("some text");
ll.addView(okButton, layoutParams);
Mauro
Its possible to give the margins for the widgets in code too.
Vinayak.B