erdomester
erdomester

Reputation: 11829

Layout gravity left-right

I have a linearlayout with two linearlayouts inside. Both linearlayouts have 2 buttons. I want to align the first two buttons to the left, and the other two buttons to the right. Unfortunately I have the four buttons (or the 2 layouts) side by side.

This is my code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:background="@drawable/vat_header_selector"
        android:orientation="horizontal" >

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btn_numbers"
                android:layout_width="90dp"
                android:layout_height="30dp"
                android:textColor="@drawable/vat_btn_header_textcolor_selector"
                android:background="@drawable/vat_btn_header_selector"
                android:text="Numbers">
            </Button>
            <Button
                android:id="@+id/btn_text"
                android:layout_width="90dp"
                android:layout_height="30dp"
                android:textColor="@drawable/vat_btn_header_textcolor_selector"
                android:background="@drawable/vat_btn_header_selector2"
                android:text="Words">
            </Button>   
        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btn_black"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:textColor="@drawable/vat_btn_header_textcolor_selector"
                android:background="@drawable/vat_btn_header_selector"
                android:text="B">
            </Button>
            <Button
                android:id="@+id/btn_white"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:textColor="@drawable/vat_btn_header_textcolor_selector"
                android:background="@drawable/vat_btn_header_selector2"
                android:text="W">
            </Button>   
        </LinearLayout>

    </LinearLayout>

This is how it looks like: enter image description here

And this is what I want: enter image description here

Upvotes: 4

Views: 8560

Answers (2)

user
user

Reputation: 87064

Gravity left, right doesn't work when the orientation of the LinearLayout is set to horizontal(as the views are placed along the X axis). One way to do what you want is to insert an empty View between the two LinearLayouts to create a space between them:

<View android:layout_width="0dp"
      android:layout_height="fill_parent"
      android:layout_weight="1" />

Upvotes: 10

Chirag
Chirag

Reputation: 56925

Apply below properties to Second Linear Layout which contain Button B and Button W.

android:gravity="right"
android:layout_weight="1"

Upvotes: 2

Related Questions