How to make a Button align to the right in a layout?

27,811

Solution 1

i was thinking he wanted something like this:

<?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="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <Button android:text="@+id/Button01" android:id="@+id/Button01"
        android:layout_width="wrap_content" android:layout_alignParentRight="true"
        android:layout_centerVertical="true" android:layout_height="wrap_content" />

    <LinearLayout android:orientation="vertical"
        android:layout_toLeftOf="@+id/Button01" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/TextLayout">
        <TextView android:id="@+id/toptext" android:layout_width="fill_parent"
            android:layout_height="0dip" android:layout_weight="1"
            android:gravity="center_vertical" android:textSize="18sp"
            android:textColor="#FFFFFF" android:background="#FFAABB" />

        <TextView android:layout_width="fill_parent"
            android:layout_height="0dip" android:layout_weight="1" android:id="@+id/bottomtext"
            android:singleLine="true" android:ellipsize="marquee"
            android:textSize="15sp" android:textColor="#FFFFFF"
            android:background="#FFBBAACC" />
    </LinearLayout>
</RelativeLayout>

Solution 2

Use android:gravity or setGravity(int)

Specifies how to place the content of an object, both on the x- and y-axis, within the object itself.

http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:gravity

specifically for this case, right|center_vertical

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

        <TextView
            android:id="@+id/toptext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="text centered"/>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bottomtext"
            android:text="text to the left"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn"
            android:text="the button is at the right"/>
</LinearLayout>

Solution 3

If you use RelativeLayout try

android:layout_alignParentRight="true"
android:layout_centerVertical="true"

That works for me but took a lot of tweaking to get right.

And you also may need

 android:layout_width="wrap_content"
Share:
27,811
Sheehan Alam
Author by

Sheehan Alam

iOS, Android and Mac Developer. i can divide by zero.

Updated on March 01, 2020

Comments

  • Sheehan Alam
    Sheehan Alam about 4 years

    I have a custom ListItem row that has a TextView on the top and a TextView on the bottom. I would like to create a Button that is right aligned, and in centered vertically. How can I do this in 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="?android:attr/listPreferredItemHeight"
        android:padding="6dip">
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="fill_parent">
            <TextView
                android:id="@+id/toptext"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:gravity="center_vertical"
           android:textSize="18sp" android:textColor="#FFFFFF"/>
    
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1" 
                android:id="@+id/bottomtext"
                android:singleLine="true"
                android:ellipsize="marquee"
            android:textSize="15sp" android:textColor="#FFFFFF"/>
    
    </LinearLayout>
    </LinearLayout>
    
    • jkhouw1
      jkhouw1 about 13 years
      did you want the button between the textviews? overlapping the textviews? to the right of the textviews?
    • Sheehan Alam
      Sheehan Alam about 13 years
      to the right of the textviews
    • jkhouw1
      jkhouw1 about 13 years
      thats what my proposed layout does.
  • jkhouw1
    jkhouw1 about 13 years
    I think he wants the button centered not the contents of the button
  • Aleadam
    Aleadam about 13 years
    @jkhouw1 it only depends where you add the gravity. See a full example in my answer.
  • Sheehan Alam
    Sheehan Alam about 13 years
    This displays the button on the bottom of the textviews and it is aligned left.