Question

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.

Was it helpful?

Solution

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"

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top