Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 853
PeakGen Avatar asked Nov 17 '13 14:11

PeakGen


People also ask

How do you align text in LinearLayout?

How do you align text in LinearLayout? To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”.

What is the default orientation of a LinearLayout?

The default orientation of a LinearLayout is horizontal.

What is the relevance of LinearLayout?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

What's the difference between LinearLayout and RelativeLayout?

LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.


2 Answers

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>
like image 95
wvdz Avatar answered Sep 18 '22 13:09

wvdz


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>
like image 20
Manishika Avatar answered Sep 18 '22 13:09

Manishika