How to right align widget in horizontal linear layout Android?
Asked Answered
S

20

246

This is the code I am using and it is not working:

<?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="fill_parent" 
    android:orientation="horizontal">

    <TextView android:text="TextView" android:id="@+id/textView1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:gravity="right">
    </TextView>

</LinearLayout>
Scrimmage answered 9/8, 2011 at 7:30 Comment(0)
B
487

Try to add empty View inside horizontal LinearLayout before element that you want to see right, e.g.:

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

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
Bunche answered 3/2, 2013 at 22:8 Comment(7)
This works! But anyone can explain why? I couldn't totally understand.Middlebrow
Empty View is trying to take the maximum amount of space, leaving room for the button.Bunche
So far this has been the only thing that works for me in trying to have a few buttons on the left, and one final button on the right. But it feels like a hack to me. Is there a "right" way to do this?Corporative
This is awesome! But why gravity="right" does not work this way from the very beginning? Why does it need help from this other view? And how is it "right", if it is not, without the extra view?Biotechnology
This does not work in a grid-like layout - there is no empty space. Sherif elKhatib's solution works.Seligman
Using layout_weight is overkill for such simplistic task. Look at @Sherif elKhatib's answer. It is much better performance-wise.Diverse
work for me but make no sense lol, that's why I must use constraintGangster
I
133

Do not change the gravity of the LinearLayout to "right" if you don't want everything to be to the right.

Try:

  1. Change TextView's width to fill_parent
  2. Change TextView's gravity to right

Code:

    <TextView 
              android:text="TextView" 
              android:id="@+id/textView1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"   
              android:gravity="right">
    </TextView>
Inexact answered 9/8, 2011 at 8:16 Comment(6)
thanks for this - it was the width of fill_parent that was throwing me offAgnosticism
Is the linear layout even designed to achieve this? Shouldn't one just use the relative layout to achieve this? Is this solution not a hack?Kwan
this will not work if you have multiple TextView controls in a LinearLayout. See answer from @BuncheSteenbok
This should be the accepted answer. This approach does not use any additional views, and more importantly, it does not use layout_weight that considerably shows performance down.Diverse
android:layout_weight = "1" side by side with android:gravity="right", should do the trick.Principle
@Shantanu try this android:layout_gravity="right". It worked for meVilleinage
T
81

As a supplement to alcsan's answer, you can use Space since API 14 (Android 4.0 ICE_CREAM_SANDWICH), document here.

Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.

<?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" >

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

    <TextView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="TextView"
        android:gravity="right" />

</LinearLayout>

For apps that supporting API levels under 14, there is an android.support.v4.widget.Space since Android Support Library r22.1.0.

Teodor answered 7/9, 2015 at 6:53 Comment(5)
Important is that you need to set layout_weight = "1"Toussaint
Works perfectly! Preserves the width of each element.Coadjutor
This works. Wow, Android's ugliness never ceases to amaze meAltar
I'm not sure you even need the gravity attribute. I didn't. As long as you set the layout_weight of the Space element to 1, the Space takes up whatever horizontal space is left after the TextView gets whatever space it needs, which forces the TextView all the way to the right (since the Space comes before in the xml).Elwaine
Android Studio now reports a warning when using a "Space" element in a LinearLayout Replacing with a "View" element seems to fix the issue. In my case, I changed the layout to a ConstraintLayout, which also works fineOctober
M
34

With Linear Layout

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/select_car_book_tabbar"
        android:gravity="right" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/my_booking_icon" />
    </LinearLayout>

enter image description here

with FrameLayout

<FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/select_car_book_tabbar">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|right"
            android:src="@drawable/my_booking_icon" />
    </FrameLayout>

with RelativeLayout

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/select_car_book_tabbar">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:src="@drawable/my_booking_icon" />
    </RelativeLayout>
Malicious answered 23/10, 2013 at 5:37 Comment(0)
T
27

setting the view's layout_weight="1" would do the trick.!

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Transit answered 23/3, 2015 at 8:45 Comment(5)
Such a simple solution +1Prostatectomy
If someone is still looking, this is the right solution :)Daigle
This is Magic! How did you find it out?Eximious
@andreikashin, Consider a up vote if it helped. Thanks.Transit
This is not a solution for components such as buttonsTraynor
B
15

Add android:gravity="right" to LinearLayout. Assuming the TextView has layout_width="wrap_content"

Brabazon answered 9/8, 2011 at 7:40 Comment(1)
He specifically said to align a single component inside the LinearLayout. Not the linear layout itself : the layout itself is set to fill_parent.Abelabelard
P
11

just add android:gravity="right" in your Liner Layout.

Protagoras answered 11/4, 2017 at 12:20 Comment(0)
G
10

For LinearLayout with horizontal orientation, give layout_weight to other child view except for the view that you want to align right. This works perfectly.

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Specialization"
                />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Right"
                android:textColor="#ff0000" />
    
    </LinearLayout>
Galliett answered 16/12, 2020 at 5:34 Comment(0)
G
5

I have done it by easiest way:

Just take one RelativeLayout and put your child view in it, which you want to place at right side.

    <LinearLayout
        android:id="@+id/llMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f5f4f4"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingBottom="20dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingTop="20dp">

        <ImageView
            android:id="@+id/ivOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />


        <TextView
            android:id="@+id/txtOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="Hiren"
            android:textAppearance="@android:style/TextAppearance.Medium"
            android:textColor="@android:color/black" />

        <RelativeLayout
            android:id="@+id/rlRight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right">


            <ImageView
                android:id="@+id/ivRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:src="@drawable/ic_launcher" />

        </RelativeLayout>
    </LinearLayout>

Hope it will help you.

Greensickness answered 18/7, 2015 at 6:31 Comment(0)
B
4

linear layout with layout_width="fill_parent" and also the widget with same layout width + gravity as right would align it to the right.

I'm using 2 TextViews in following example, topicTitle on the left and topicQuestions on the right.

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:orientation="horizontal">

    <TextView
        android:id="@+id/topicTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/topicQuestions"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textSize="18sp"
        android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>

Output

Blair answered 2/1, 2017 at 2:38 Comment(0)
O
3

No need to use any extra view or element:

//that is so easy and simple

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

//this is left alignment

<TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="No. of Travellers"
      android:textColor="#000000"
      android:layout_weight="1"
      android:textStyle="bold"
      android:textAlignment="textStart"
      android:gravity="start" />

//this is right alignment

<TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Done"
      android:textStyle="bold"
      android:textColor="@color/colorPrimary"
      android:layout_weight="1"
      android:textAlignment="textEnd"
      android:gravity="end" />

</LinearLayout>
Of answered 12/10, 2018 at 6:54 Comment(0)
C
2

You should use a RelativeLayout and just drag them until it looks good :)

    <ImageView
        android:id="@+id/button_info"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:contentDescription="@string/pizza"
        android:src="@drawable/header_info_button" />

</RelativeLayout>
Cuneo answered 16/10, 2012 at 11:31 Comment(1)
"drag them" not recommended for multi resolution screenEllary
A
2

Try to change the layout_width to android:layout_width="match_parent" because gravity:"right" aligns the text inside the layout_width, and if you choose wrap content it does not have where to go, but if you choose match parent it can go to the right.

Arella answered 21/5, 2015 at 6:51 Comment(0)
A
1

Adding view is bit difficult and it cover all screen width like this:

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

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

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Try to this code:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create Account"/>

</LinearLayout>
Abseil answered 16/9, 2018 at 2:2 Comment(0)
S
0

In case with TextView:

<TextView 
    android:text="TextView" 
    android:id="@+id/textView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"   
    android:gravity="right"
    android:textAlignment="gravity">    
</TextView>
Sacrifice answered 7/11, 2016 at 21:59 Comment(0)
T
0

For aligning one element at start and one at the end of the LinearLayout, you can wrap it in an RelativeLayout.

 <androidx.appcompat.widget.LinearLayoutCompat
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="8dp"
    android:weightSum="2">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="start">
        <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel"
            android:textColor="@android:color/background_dark"
            android:backgroundTint="@android:color/transparent"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="end">
        <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/background_dark"
            android:backgroundTint="@android:color/transparent"
            android:text="Save"/>
    </RelativeLayout>
</androidx.appcompat.widget.LinearLayoutCompat>

The result of this example is following: Link to the image

Note: You can wrap whatever you want inside and align it.

Thiamine answered 3/9, 2020 at 13:25 Comment(0)
S
0

this is my xml, dynamic component to align right, in my case i use 3 button

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/checkinInputCodeMember">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="7"
                android:orientation="vertical" />

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/bttn_extends"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:textColor="@color/colorAccent"
                android:text="3"/>

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/bttn_checkout"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:textColor="@color/colorAccent"
                android:text="2"/>

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/checkinButtonScanQrCodeMember"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:textColor="@color/colorAccent"
                android:text="1"/>


        </LinearLayout>

and the result

you can hide the right first button with change visibility GONE, and this my code

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/checkinInputCodeMember">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="7"
                android:orientation="vertical" />

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/bttn_extends"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:textColor="@color/colorAccent"
                android:text="3"/>

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/bttn_checkout"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:textColor="@color/colorAccent"
                android:text="2"/>

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/checkinButtonScanQrCodeMember"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="1"
                android:textColor="@color/colorAccent"
                **android:visibility="gone"**/>


        </LinearLayout>

still align right, after visibility gone first right component

result code 1

result code 2

Santiagosantillan answered 16/10, 2020 at 2:44 Comment(0)
S
-1

Here is a sample. the key to arrange is as follows

android:layout_width="0dp"
android:layout_weight="1"

Complete code

<?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="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp">

    <TextView
        android:id="@+id/categoryName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="abcd" />

    <TextView
        android:id="@+id/spareName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="efgh" />

</LinearLayout>

enter image description here

Syllabify answered 22/9, 2017 at 14:0 Comment(0)
L
-1

Use match_parent and gravity to set the TextView text to right, like this:

<?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">

    <TextView android:text="TextView" android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">
    </TextView>

</LinearLayout>
Leader answered 22/1, 2018 at 20:56 Comment(0)
C
-2

Try This..

<?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="fill_parent" 
    android:orientation="horizontal" 
    android:gravity="right" >



    <TextView android:text="TextView" android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>

</LinearLayout>
Cherie answered 21/2, 2014 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.