PeakGen
PeakGen

Reputation: 23005

LinearLayout: Aligning to the Right of the parent

Pleae have a look at the following XML

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

    <TextView
        android:id="@+id/open_file_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:gravity="left"
        android:padding="5dp"
        android:text="TextView" />


    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="@string/open" />

</LinearLayout>

I want to align the Button to the right corner of the layout. How can I do this?

Upvotes: 8

Views: 28846

Answers (5)

wvdz
wvdz

Reputation: 16641

Another possible way of doing this is by using the layout_weight attribute. This has my preference, because it fills up all available space.

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

    <TextView
        android:id="@+id/open_file_name"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="match_parent"
        android:layout_gravity="start|left"
        android:gravity="start|left"
        android:padding="5dp"
        android:text="TextView" />


    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="end|right"
        android:text="open" />

</LinearLayout>

Upvotes: 9

vinoth h
vinoth h

Reputation: 531

Just try this,it worked for me.

    <TextView
        android:id="@+id/open_file_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:gravity="left"
        android:padding="5dp"
        android:text="TextView" />

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

Upvotes: 0

jagmohan
jagmohan

Reputation: 2052

Try this as well.

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

<TextView
    android:id="@+id/open_file_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"        
    android:layout_gravity="left"
    android:padding="5dp"
    android:text="TextView" />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text="@string/open" />

</LinearLayout>

Upvotes: 2

Blacklight
Blacklight

Reputation: 3827

You can add a filling View between the TextView and the Button using the layout_weight attribute:

<View android:layout_width="0dp"
      android:layout_heigth="wrap_content"
      android:layout_weight="1"/>

Or better use a RelativeLayout and android:layout_alignParentRight="true".

Upvotes: 1

Manishika
Manishika

Reputation: 5564

Use RelativeLayout for this android:layout_alignParentLeft="true" and android:layout_alignParentRight="true"

Try below data

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

    <TextView
        android:id="@+id/open_file_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="5dp"
        android:text="TextView" />

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

    </RelativeLayout>

Upvotes: 7

Related Questions