Gravity attributes defined in Android are

  • android:gravity : Sets the gravity of the contents (i.e. its subviews) of the View it’s used on. Gravity arranges the content inside the view.
  • android:layout_gravity : Sets the gravity of the View or Layout relative to its parent. It arranges a view in its layout.

So gravity arranges the content inside the view. layout_gravity arranges the view’s position outside of itself. Important points are

  • layout_gravity does not work for views in a RelativeLayout. It is only supported for views in a LinearLayout or FrameLayout.
  • View width and/or height has to be greater than its content. Otherwise gravity won’t have any effect. So wrap_content and gravity should not be used together.
  • View width and/or height has to be less than the parent. Otherwise layout_gravity won’t have any effect. So match_parent and layout_gravity should not be used together.

Placing View

Commonly used constant used in android:gravity and android:layout_gravity for placing an object within a potentially larger container are given in below table.

ConstantDescription
centerPlace the object in the center of its container in both the vertical and horizontal axis, not changing its size.
center_horizontalPlace object in the horizontal center of its container, not changing its size.
center_verticalPlace object in the vertical center of its container, not changing its size.
clip_horizontalLeft and/or right edges of the child clipped to its container’s bounds based on the horizontal gravity. A left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.
clip_verticalTop and/or bottom edges of the child clipped to its container’s bounds based on the vertical gravity. A top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
startPush object to the beginning of its container, not changing its size.
endPush object to the end of its container, not changing its size.
leftPush object to the left of its container, not changing its size.
rightPush object to the right of its container, not changing its size.
topPush object to the top of its container, not changing its size.
bottomPush object to the bottom of its container, not changing its size.
fillGrow the horizontal and vertical size of the object if needed so it completely fills its container.
fill_horizontalGrow the horizontal size of the object if needed so it completely fills its container.
fill_verticalGrow the vertical size of the object if needed so it completely fills its container.

To specify multiple constraint, separate them by ‘|’.

Example

Below example demonstrate the impact of gravity constraint on android:gravity and android:layout_gravity.

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#e3e2ad"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="24sp"
            android:text="gravity=" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="left"
            android:text="left" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center_horizontal"
            android:text="center_horizontal" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="right"
            android:text="right" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center"
            android:text="center" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#d6c6cd"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="24sp"
            android:text="layout_gravity=" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="left"
            android:background="#bcf5b1"
            android:text="left" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:background="#aacaff"
            android:text="center_horizontal" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="right"
            android:background="#bcf5b1"
            android:text="right" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:background="#aacaff"
            android:text="center" />

    </LinearLayout>
</LinearLayout>