0

I am creating a layout dynamically, I am using

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

to place my elements one below the other. But for my header, i am using a new layout

LinearLayout layout_header = new LinearLayout(this);             
layout.setOrientation(LinearLayout.HORIZONTAL);  

and adding this in the view of the LinearLayout layout. But in LinearLayout layout_header, i have two buttons and i want one of them to be in total left and the other in total right. But both are coming side by side, i tried to include padding/margins but didn work.

I also created a layout params for both the buttons and tried adding margins/gravity/padding individually, but still did not work.

Please help how to proceed to place these buttons at two separate corners.

bharath
  • 953
  • 4
  • 17
  • 30

2 Answers2

1

If you want one thing at the far left and one at the far right, its easier to use a relative layout. Just make the first android:layout_alignParentRight="true" and the other android:layout_alignParentLeft="true"

Pragnani
  • 20,075
  • 6
  • 49
  • 74
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • thanks, but can we set this programatically?? i am generating the view by code, relative layout is fine for me! – bharath Mar 14 '13 at 16:20
  • 1
    Yes, you'd have to put it in a RelativeLayout and use the RelativeLayout.LayoutParams class to specify the constraints above. Just add the right rule to the params for each. – Gabe Sechan Mar 14 '13 at 16:23
0

I'd recommend Gabe's answer, since that's precisely what RelativeLayouts are for. However, if you want to keep it a LinearLayout for whatever reason, you can place an invisible View between your two Buttons:

<Button
    ... />
<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />
<Button
    ... />

The layout_weight attribute specifies that it should take up the remaining room. Just be sure to use 0dp for the width, so that doesn't interfere.

Geobits
  • 22,218
  • 6
  • 59
  • 103