Joseph
Joseph

Reputation: 2726

Android Layout left and right align in horizontal layout

I am have a ImageView and a ImageButton. I have them next to each other in a horizontal layout. I am trying to make it so that the image is left aligned on the screen, and the button is right aligned. I have tried setting gravity but it doesnt seem to make a difference. Where am I going wrong?

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:gravity="left"
        android:src="@drawable/short_logo" />

    <ImageButton
        android:id="@+id/terminateSeed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:background="@null"
        android:src="@drawable/unregister_icon" />
</LinearLayout>

Upvotes: 22

Views: 61499

Answers (7)

Benny
Benny

Reputation: 2331

Another approach for LinearLayout, using layout_weight and gravity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="BUTTON 1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="BUTTON 2" />
</LinearLayout>

Upvotes: 0

Gopi Cg
Gopi Cg

Reputation: 382

Use This example if you want to use LinearLayout without giving Weight

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
       />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="right"
        />
    </LinearLayout>

Upvotes: 3

Arif Nadeem
Arif Nadeem

Reputation: 8604

Use...

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1.0" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:layout_marginRight="80dp"
    android:layout_weight="0.5"
    android:gravity="left"
    android:src="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/terminateSeed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="right"
    android:layout_marginLeft="80dp"
    android:background="@null"
    android:src="@drawable/ic_launcher" />
</LinearLayout>

Upvotes: 12

josephus
josephus

Reputation: 8304

a RelativeLayout would solve your problem with a snap, but if you still insist on using LinearLayout (just because we're badass and do things the hard way), you do it like this:

Your parent LinearLayout is fill-parent, left-aligned, and horizontally oriented, and contains your ImageView, and another LinearLayout.

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"

Second LinearLayout is fill-parent, right-aligned, and horizontally oriented, and contains your ImageButton.

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"

Upvotes: 0

Sunny
Sunny

Reputation: 14808

Make imageview and imagebutton wrap_content and set layout_gravity to left and right accordingly.

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

<RelativeLayout 
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"

>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"

    android:layout_alignParentLeft="true"
    android:src="@drawable/short_logo" />

<ImageButton
    android:id="@+id/terminateSeed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="@null"
    android:src="@drawable/unregister_icon" />
</RelativeLayout >

Upvotes: 0

MAC
MAC

Reputation: 15847

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

Upvotes: 33

Related Questions