Android Gravity

Gravity provides the standard constants and tools for placing an object within a larger container.

The Android RelativeLayout attribute enables you to specify how child views are positioned relative to each other.

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.

What is android:gravity?

  • android:gravity is an attribute that sets the gravity of the content of the view its used on.
  • The android:gravity specifies how an object should position its content on both X and Y axis.
  • The possible values of android:gravity are top, bottom, left, right, center, center_vertical, center_horizontal etc.
  • The android:gravity is used to control gravity of all child views of a container.
  • It is the inside gravity of the View.
  • It specifies the direction in which its content should align.
  • It controls your view placed inside the parent.

  • Example : android:gravity=“center”

    <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:gravity="center_vertical"
             android:orientation="vertical" >
        <EditText
             android:id="@+id/enterNumberEdit"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_margin="10dp"
             android:hint="Enter No." >

        <requestFocus />
        </EditText>
    </LinearLayout>


  • In the above example, android:gravity aligns the child of LinearLayout in center.
  • The android:gravity is for child components.
  • It is not a Layout Parameter. It is an attribute of the View Group.

What is android:layout_gravity?

  • The android:layout_gravity is an attribute that sets the gravity of the View or Layout in its parent.
  • The android:layout_gravity is used to control the gravity of an individual view in a container.
  • It is the outside gravity of the View.
  • It specifies the direction in which the View should touch it's parent's border.

  • Example : android:layout_gravity=“center”

    <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_gravity="center"
           android:orientation="vertical"
           android:background="@drawable/dialog_background" >
    </LinearLayout>


  • In the above example, android:layout_gravity aligns the child of LinearLayout in center.
  • The android:layout_gravity is for child parent components.
  • It is a Layout Parameter. All View groups do not support this Layout Param.
  • The LinearLayout does support android:layout_gravity Layout Param.

LinearLayout

  • LinearLayout is a view group that aligns all children in a single direction, vertically, horizontally.
  • It is a commonly used view group that lays out its children views either horizontally or vertically.

Example : Demonstrating android:gravity and android:layout_gravity

File Name: content_main.xml

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayoutxmlns: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 = "match_parent"
app:layout_behavior = "@string/appbar_scrolling_view_behavior"
tools:context = "com.example.custom.and gravity.MainActivity"
tools:showIn = "@layout/activity_main"
android:orientation = "vertical"
>

<Button
android:layout_width = "130dp"
android:layout_height = "130dp"
android:text = "Button1"
android:id = "@+id/button1"
android:gravity = "top"
android:textSize = "20dp"
android:background = "#FF0000"
/>

<Button
android:layout_width = "130dp"
android:layout_height = "130dp"
android:text = "Button2"
android:id = "@+id/button2"
android:gravity = "bottom"
android:textSize = "20dp"
android:background = "#00FF00"
/>

<Button
android:layout_width = "130dp"
android:layout_height = "130dp"
android:text = "Button3"
android:id = "@+id/button3"
android:textSize = "20dp"
android:layout_gravity = "right"
android:gravity = "top"
android:background = "#FFFF00"
/>
</LinearLayout>


Output:

android gravity

Note:
  • You can change or modify the button's position using gravity attribute of the button control.
  • Do not use android:gravity or android:layout_gravity with a RelativeLayout. Use them for Views in LinearLayouts and FrameLayouts.