141

If you look at the attached image. I need my button to be right aligned but for some reason it's not working with 'gravity:right'...

Cancel Add

Here's my code for that layout:

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

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_marginTop="9dp" />

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />

</LinearLayout>

Why is no working?!

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

18 Answers18

168

Single LinearLayout solution. Only adding a simple spacing view:
(No-one seemed to have mentioned this one, only more complicated multi-layout solutions.)

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

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_marginTop="9dp" />

    <!-------------------------  ADDED SPACER VIEW -------------------->
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <!------------------------- /ADDED SPACER VIEW -------------------->

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />

</LinearLayout>

Note the "highlighted" View, I didn't modify anything else. Height=0 makes sure it's not visible. Width=0 is because the width is determined by the LinearLayout based on weight=1. This means that the spacer view will stretch as much as possible in the direction (orientation) of the LinearLayout.

Note that you should use android.widget.Space or android.support.v4.widget.Space instead of View if your API level and/or dependencies allow it. Space achieves the job in a cheaper way, because it only measures and doesn't try to draw anything like View does; it's also more expressive conveying the intention.

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • 2
    Just add android:layout_weight="1" to R.id.lblExpenseCancel and that's all. – hector6872 May 02 '15 at 13:08
  • 1
    @h_rules that works if you want to hack, but imagine what happens when you add background to the TextView. – TWiStErRob May 02 '15 at 13:39
  • 3
    I wonder why this answer got downvote, it works and it is nice, I'm gonna try it definitely. – Elvedin Hamzagic May 23 '15 at 13:44
  • That is perfect as a mobile developer – Shivanand Darur Jul 28 '16 at 09:23
  • If you add a spacer, then in different screen sizes you have different result. So its not the ideal solution. – GeoMint Sep 11 '16 at 10:36
  • @GeoMint what is different for you on different screen sizes? Doesn't "some view on the left and a button aligned to right next to it" always means the same? Even if the space between the two views grows... is your use case different than OP? – TWiStErRob Sep 11 '16 at 11:07
  • Spacer has static size. In different devices/screens has different view. He needs something more dynamic. – GeoMint Sep 11 '16 at 17:29
  • 1
    @GeoMint `layout_weight="1"` gives it dynamic size, that means "be as wide as you can" (because the `LinearLayout` is `horizontal`). [width=`0dp` is just for performance](http://stackoverflow.com/q/12016781/253468), it won't be actually 0 wide. I have a feeling you didn't even try the code... – TWiStErRob Sep 11 '16 at 21:00
  • this answer should be upvoted is a great answer... nice work... – Alex Rivas Aug 19 '20 at 06:02
  • My fellow developer is genius. Thank you so much. – Mandeep Kumar Mar 31 '23 at 09:50
145

Use below code for that

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="9dp"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/stitch_button"
        android:text="@string/add" />

</RelativeLayout>
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
44

Real solution for it case:

android:layout_weight="1" for TextView, and your button move to right!

nicolas asinovich
  • 3,201
  • 3
  • 27
  • 37
30

I know this question was asked awhile ago, but I have done this before and it allows for more control when aligning items.If you look at it like web programming, it helps. You just nest another LinearLayout that will contain your button inside of it. You can then change the gravity of the button's layout and make it go to the right while the TextView is still on the left.

Try this code:

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

<TextView
    android:id="@+id/lblExpenseCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cancel" 
    android:textColor="#404040"         
    android:layout_marginLeft="10dp" 
    android:textSize="20sp" 
    android:layout_marginTop="9dp"/>

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

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"                    
        android:text="@string/add" 
        android:layout_gravity="right" 
        android:layout_marginRight="15dp"/>

</LinearLayout>

You may have to play with the padding on the nested layout so that it acts how you want it.

BobbyD17
  • 495
  • 5
  • 16
  • 2
    You should use a FrameLayout for your nested layout. To position one single element is what it's for. And also, give it a weight so it fills the right part of the parent. +1 for not using Relative Layout anyway. – Hjalmar Jun 22 '15 at 19:52
11

try this one

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

  <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/lblExpenseCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="9dp"
    android:text="cancel"
    android:textColor="#ffff0000"
    android:textSize="20sp" />

<Button
    android:id="@+id/btnAddExpense"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="15dp"
     android:textColor="#ff0000ff"
    android:text="add" />

 </RelativeLayout>
  </LinearLayout>
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
9

As mentioned a couple times before: To switch from LinearLayout to RelativeLayout works, but you can also solve the problem instead of avoiding it. Use the tools a LinearLayout provides: Give the TextView a weight=1 (see code below), the weight for your button should remain 0 or none. In this case the TextView will take all the remaining space, which is not used to display the content of your TextView or ButtonView and pushes your button to the right.

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

        <TextView
            android:id="@+id/lblExpenseCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancel"
            android:textColor="#404040"
            android:layout_marginLeft="10dp"
            android:textSize="20sp"
            android:layout_marginTop="9dp"

            **android:layout_weight="1"**

            />

        <Button
            android:id="@+id/btnAddExpense"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:background="@drawable/stitch_button"
            android:layout_marginLeft="10dp"
            android:text="@string/add"
            android:layout_gravity="right"
            android:layout_marginRight="15dp" />

    </LinearLayout>
Barb
  • 91
  • 1
  • 4
  • 2
    This is the best answer. I just wanted to write something similar until I found your post. – codingdave Jul 30 '17 at 22:56
  • That is way better than switching to `RelativeLayout` due to memory issues, since the `RelativeLayout` consumes way more memory than `LinearLayout` – ThunderWiring Sep 27 '17 at 09:11
7

You need to add gravity to the layout not the Button, gravity in button settings is for Text inside the button

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_gravity="right" 
android:layout_height="wrap_content"             
android:orientation="horizontal" 
android:layout_marginTop="35dp">
goodm
  • 7,275
  • 6
  • 31
  • 55
  • 3
    Wouldn't that then align the TextView right as well? I just need the button right aligned... –  May 31 '12 at 08:38
  • 1
    If you need to align only button, use RelativeLayout. – goodm May 31 '12 at 08:44
  • 1
    Even if you want all buttons aligned, this doesn't work (API 16). Children views are still aligned on left. – m0skit0 May 17 '17 at 18:24
5
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"             
    android:orientation="horizontal" 
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:text="@string/cancel" 
        android:textColor="#404040"         
        android:layout_marginLeft="10dp" 
        android:textSize="20sp" 
        android:layout_marginTop="9dp"/>              

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"                    
        android:text="@string/add" 
        android:layout_gravity="right" 
        android:layout_marginRight="15dp"/>

</LinearLayout>

This will solve your problem

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
Anand S Joshi
  • 51
  • 1
  • 1
5

Just add android:gravity="right" this line parent layout this will work.

<LinearLayout
        android:id="@+id/withdrawbuttonContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        android:orientation="horizontal"
        **android:gravity="right"**
        android:weightSum="5"
        android:visibility="visible">

        <Button
            android:id="@+id/bt_withDraw"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/BTN_ADD"
            app:delay="1500" />

        <Button
            android:id="@+id/btn_orderdetail_amend"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/BTN_CANCEL"
            app:delay="1500" />
</LinearLayout>
velraj
  • 261
  • 4
  • 3
5

Joining the party very late, but the standard way is to use <Space>, which was literally created for this very purpose.

So in your code, just insert the following between the TextView and the Button:

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

Or from the palette, just select Layouts > Space. It will dump it just as it is in the code above, you'll just need to specify the weight.

enter image description here

Adam
  • 163
  • 1
  • 5
4

If you don't want to, or can't, use RelativeLayout, you can wrap the button in a LinearLayout with orientation "vertical" and width "fill_parent".

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

  <TextView
      android:id="@+id/lblExpenseCancel"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/cancel"
      android:textColor="#404040"
      android:layout_marginLeft="10dp"
      android:textSize="20sp"
      android:layout_marginTop="9dp" />

  <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">
     <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />
  </LinearLayout>
</LinearLayout> 

This is because if the LinearLayout's orientation is horizontal, gravity will only affect the views vertically. And if the orientation is 'vertical', gravity will only affect the views horizontally. See here for more details on the LinearLayout orientation/gravity explanation.

0

I have used a similar layout with 4 TextViews. Two TextViews should be aligned to left and two TextViews should be aligned to right. So, here is my solution, if you want to use LinearLayouts alone.

<?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:baselineAligned="false"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="0dip"
        android:layout_weight="0.50"
        android:layout_height="match_parent"
        android:gravity="left"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_level"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/level"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_level_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_weight="0.50"
        android:layout_height="match_parent"
        android:gravity="right"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/time"
            android:textAppearance="?android:attr/textAppearanceMedium"
             />

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_time_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium" 
            />
    </LinearLayout>

</LinearLayout>
Vamsi Challa
  • 11,038
  • 31
  • 99
  • 149
0

You can use RelativeLayout or set gravity="right" on the parent layout of your Button.

HuynhHan
  • 365
  • 1
  • 4
  • 11
0

I know this is old but here is another one in a Linear Layout would be:

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

<TextView
    android:id="@+id/lblExpenseCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cancel"
    android:textColor="#404040"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:layout_marginTop="9dp" />

<Button
    android:id="@+id/btnAddExpense"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:background="@drawable/stitch_button"
    android:layout_marginLeft="10dp"
    android:text="@string/add"
    android:layout_gravity="center_vertical|bottom|right|top"
    android:layout_marginRight="15dp" />

Please note the layout_gravity as opposed to just gravity.

Juanjo
  • 670
  • 7
  • 17
0

You should use the Relativelayout instead of Linearlayout as main layout. If you use Relativelayout as main then easily handle the button on right side because relative layout provide us alignParent right, left,top and bottom.

0
    Adding Spacing as View , makes the Last textview to right in 
    LinearLayout

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
     <!-- ADD SPACER VIEW -->
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            />
    <!-- /ADD SPACER VIEW -->
</LinearLayout>
0

This will work without adding any additional views.

You need to set your linear-layout height to wrap-content so that it will occupy the maximum size of the contained widgets. Then we will align it using gravity set to center_vertical.

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:gravity="center_vertical"
    android:paddingHorizontal="8dp"
    tools:context=".MainActivity">

    <Button
        android:paddingVertical="40"
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start"
        />

    <Button
        android:id="@+id/pauseButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pause"
        />

    <Button
        android:id="@+id/resetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reset"
        />

</androidx.appcompat.widget.LinearLayoutCompat>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
UTKARSH Sharma
  • 698
  • 1
  • 8
  • 17
-2

Use layout width in the button like android:layout_width="75dp"