Gravity:
Gravity is an Android method for aligning view in a Layout. There are two kind of Gravity -
android:gravity:
android:gravity arranges the content inside the view.
android:gravity="right"
android:layout_gravity:
android:layout_gravity arranges the view's position outside of itself:
android:layout_gravity="right"
android:layout & android:layout_gravity:
We can use both kind of gravity to set views. android:layout_gravity for the view, and android.layout for the content inside the view.
android:gravity="center"
android:layout_gravity="right"
The Position of each view can be specified as relative to sibling elements or to the parent. Gravity is used to align its child to a specific position. The values of both attributes is combination of the following constants:
Constant | Description |
---|---|
top | Push elements to the top of its container without changing its size. |
bottom | Push elements to the bottom of its container without changing its size. |
left | Push elements to the left side of its container without changing its size. |
right | Push elements to the right side of its container without changing its size. |
center_vertical | Place elements in the vertical center of its container without changing the size. |
fill_vertical | Grow the Vertical size of the element if needed so it completely fills its container. |
center_horizontal | place object in the horizontal center of its container without changing its size. |
fill_horizontal | Grow the horizontal size of the element if needed so it completely fills its container. |
center | Place the element in the center of its container in both the vertical and horizontal axis, without changing the size. |
fill | Grow the horizontal and vertical size of the element if needed so it completely fills its container. |
clip_vertical | Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip will be 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. |
clip_horizontal | Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip will be 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. |
start | Push the element to the beginning of its container without changing the size. |
end | Push the element to the end of its container without changing the size. |
android:gravity:
android:gravity arranges the content inside the view. It's equivalent to text-align property of css and specifies how an element should position its content on both X and Y axis.
<?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="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#008080"
android:textColor="black"
android:textSize="20sp"
android:gravity="left"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#08f9bd"
android:textColor="black"
android:textSize="20sp"
android:gravity="center_horizontal"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#f418d2"
android:textColor="black"
android:textSize="20sp"
android:gravity="right"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:textColor="black"
android:textSize="20sp"
android:gravity="center"
android:text="center" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#c443bf"
android:textColor="black"
android:textSize="20sp"
android:gravity="end"
android:text="end" />
</LinearLayout>
</LinearLayout>
android:layout_gravity:
android:layout_gravity arranges the view's position outside of itself. It's equivalent to float property of css and sets the gravity of the view or layout in its parent.
<?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="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#008080"
android:textColor="black"
android:textSize="20sp"
android:layout_gravity="left"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#08f9bd"
android:textColor="black"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#f418d2"
android:textColor="black"
android:textSize="20sp"
android:layout_gravity="right"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:textColor="black"
android:textSize="20sp"
android:layout_gravity="center"
android:text="center" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#c443bf"
android:textColor="black"
android:textSize="20sp"
android:layout_gravity="end"
android:text="end" />
</LinearLayout>
</LinearLayout>