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.
Constant | Description |
---|---|
center | Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. |
center_horizontal | Place object in the horizontal center of its container, not changing its size. |
center_vertical | Place object in the vertical center of its container, not changing its size. |
clip_horizontal | Left 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_vertical | Top 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. |
start | Push object to the beginning of its container, not changing its size. |
end | Push object to the end of its container, not changing its size. |
left | Push object to the left of its container, not changing its size. |
right | Push object to the right of its container, not changing its size. |
top | Push object to the top of its container, not changing its size. |
bottom | Push object to the bottom of its container, not changing its size. |
fill | Grow the horizontal and vertical size of the object if needed so it completely fills its container. |
fill_horizontal | Grow the horizontal size of the object if needed so it completely fills its container. |
fill_vertical | Grow 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>