Sheehan Alam
Sheehan Alam

Reputation: 60859

How can I make a two images sit on opposite ends using a LinearLayout?

I have 2 images in a horizontal linear layout. I want one to sit on the left and another to sit on the very right. Like this

X----------------------------X

Here is my XML

<LinearLayout
            android:id="@+id/headerBarLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >


            <ImageView
                android:id="@+id/batteryImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/battery" android:layout_weight="0"/>

            <ImageView
                android:id="@+id/exit_button"
                style="@style/MotionMetrics.ExitButton"
                android:src="@drawable/exit_button" android:layout_width="wrap_content"/>

        </LinearLayout>

Not sure why they aren't on opposite ends.

Photo:

enter image description here

Upvotes: 1

Views: 2922

Answers (3)

LostPuppy
LostPuppy

Reputation: 2501

Set the android:layoutandroid:layout_gravity="left" and the other to right. It works I tested it.

Upvotes: -1

mohamed
mohamed

Reputation: 101

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<ImageView android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:src="@drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/batteryImageView" android:layout_gravity="right"></ImageView>
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" android:id="@+id/exit_button" android:layout_alignParentTop="true"></ImageView>

Upvotes: 0

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

Use a relative layout for this. Like so:

<RelativeLayout
        android:id="@+id/headerBarLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <ImageView
            android:id="@+id/batteryImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/battery" android:layout_weight="0"
            android:layout_alignParentLeft="true"
         />

        <ImageView
            android:id="@+id/exit_button"
            style="@style/MotionMetrics.ExitButton"
            android:src="@drawable/exit_button" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
         />

    </RelativeLayout>

Upvotes: 3

Related Questions