Topic :Layout Attribute - Gravity: android:gravity | android:layout_gravity

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:gravity-example : infobrother

android:layout_gravity:

android:layout_gravity arranges the view's position outside of itself:


android:layout_gravity="right"


android:layout_gravity-example : infobrother

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"


android:layout_gravity and android:gravity-example: infobrother

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:


ConstantDescription
topPush elements to the top of its container without changing its size.
bottomPush elements to the bottom of its container without changing its size.
leftPush elements to the left side of its container without changing its size.
rightPush elements to the right side of its container without changing its size.
center_verticalPlace elements in the vertical center of its container without changing the size.
fill_verticalGrow the Vertical size of the element if needed so it completely fills its container.
center_horizontalplace object in the horizontal center of its container without changing its size.
fill_horizontalGrow the horizontal size of the element if needed so it completely fills its container.
centerPlace the element in the center of its container in both the vertical and horizontal axis, without changing the size.
fillGrow the horizontal and vertical size of the element if needed so it completely fills its container.
clip_verticalAdditional 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_horizontalAdditional 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.
startPush the element to the beginning of its container without changing the size.
endPush 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:gravity : infobrother

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>



android:layout_gravity : infobrother

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement