0

I'm creating a LinearLayout programmatically and I'm adding to this layout three buttons, but they're showed one on top of the other.

How I can show the buttons in line?

Reading around I probably understood that I need to set up a LayoutParams but I didn't figured out how..

I've tried with this but it didn't did the trick..

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
);
ll.addView(b1, layoutParams);
ll.addView(b2, layoutParams);
ll.addView(b3, layoutParams);

Thanks for any help!

EDIT:

Probably I needed to add more details.

I have also other stuff in the Layout but I does'n matter, I've created an additional layout just for the buttons.

Now the buttons are in line but they have different width.. : /

I've tried with this but it didn't help..

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            1f
);
Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • Hey. I am trying to do something similar to you, and I am having some issues. I went through this solution and checked what you did, but for some reason I am getting 4 large buttons taking up the full width of the screen, and not appearing in the same row. Do you think you could have a look and tell me what I'm doing wrong? http://pastebin.com/26cTEqhg Thanks – MasterGberry Apr 13 '13 at 18:50

4 Answers4

3
ll.setOrientation(LinearLayout.HORIZONTAL);

Lets you set buttons in horizontal line allignment.

But to give balanced space to all three buttons. you must set weight property for all the three Button objects to 1.

Edit:

Do this for all buttons.

LinearLayout.LayoutParams params = button.getLayoutParams();
params.weight = 1;
button.setLayoutParams(params);

to set weight for all buttons.

Regards, Aqif Hamid

Aqif Hamid
  • 3,511
  • 4
  • 25
  • 38
  • Yeah, that's right. Now I've my buttons in line but they're are of different width. How can I adjust the weight property? – Enrichman Jun 30 '12 at 12:24
  • in your android layout, add this to your `Button`'s properties `android:layout_weight="1"` – Aqif Hamid Jun 30 '12 at 12:31
  • How can I this progammatically? – Enrichman Jun 30 '12 at 12:42
  • Sorry to bother you man but it doesn't work.. the button.getLayoutParams() gives you a ViewGroup.LayoutParams that doesn't have the weight.. : / – Enrichman Jun 30 '12 at 13:36
  • I did it! Just added this to each button: b2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,1)); and setted the parent layout weigth to 3! ; ) – Enrichman Jun 30 '12 at 13:43
2

Try this:

ll.setOrientation(LinearLayout.HORIZONTAL);
Geralt_Encore
  • 3,721
  • 2
  • 31
  • 46
0

All have MATCH_PARENT should have LayoutParams.WRAP_CONTENT in width or height (in at least one as per LinearLayout orientation )

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

Try it:

    LinearLayout gvDivisao = (LinearLayout) findViewById(R.id.gvDivisao);
    LayoutInflater inflater = getLayoutInflater();
    LayoutParams btDivLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);

    Button btDivA = (Button)inflater.inflate(R.layout.button_divisao, null);
    btDivA.setText("A");
    gvDivisao.addView( btDivA, btDivLayoutParams);
fpauer
  • 356
  • 3
  • 9