Open In App

What is the Difference Between GRAVITY and LAYOUT_GRAVITY in Android?

Last Updated : 31 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Aligning the views while designing an application is a quite tedious task. Isn’t it? Here comes layout attributes to the rescue! Each layout has a set of attributes that define the visual properties of that layout. There are few common attributes among all the layouts and there are other attributes that are specific to that layout. Now let us take a deeper look inside the most confusing layout attributes i.e. GRAVITY and LAYOUT_GRAVITY.

Difference between GRAVITY and LAYOUT_GRAVITY

1. android:gravity

It sets the gravity of the contents i.e. its subviews of the view it’s used on. The view’s width (or height) has to be greater than its content. Otherwise, gravity won’t have any effect. Thus, wrap_content and gravity are meaningless together. gravity doesn’t work well on a RelativeLayout but can be useful with a LinearLayout.

2. android:layout_gravity 

It sets the gravity of the View or Layout relative to its parent. The layout_gravity does not work for views in a RelativeLayout. It is used for views in a LinearLayout or FrameLayout. The view’s width (or height) has to be less than the parent. Otherwise, layout_gravity won’t have any effect. Thus, match_parent and layout_gravity are meaningless together.
The layout_gravity=center looks the same as layout_gravity=center_horizontal here because they are in a vertical linear layout. You can’t center vertically in this case, so layout_gravity=center only centers horizontally.
 

In short, layout_gravity arranges a view in its layout whereas Gravity arranges the content inside the view.

What We are Going to Build in this Article?

We will be building a simple application in which we will be implementing the two concerned layouts i.e. GRAVITY and LAYOUT_GRAVITY.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with the XML Files

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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="@color/purple_200"
        android:baselineAligned="false"
        android:orientation="vertical"
        android:layout_margin="20dp">
  
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="gravity"
            android:textSize="30sp"
            android:textStyle="bold"/>
  
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:background="#bcf5b1"
            android:gravity="left"
            android:layout_margin="5dp"
            android:text="left"
            android:textSize="20sp"/>
  
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:background="#0F9D58"
            android:gravity="center_horizontal"
            android:layout_margin="5dp"
            android:text="center_horizontal"
            android:textSize="20sp"/>
  
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:background="#bcf5b1"
            android:gravity="right"
            android:text="right"
            android:layout_margin="5dp"
            android:textSize="20sp"/>
  
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:background="#0F9D58"
            android:gravity="center"
            android:text="center"
            android:layout_margin="5dp"
            android:textSize="20sp"/>
  
    </LinearLayout>
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/teal_200"
        android:orientation="vertical"
        android:layout_margin="20dp">
  
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="layout_gravity"
            android:textSize="30sp"
            android:textStyle="bold"/>
  
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="left"
            android:background="#bcf5b1"
            android:text="left"
            android:layout_margin="5dp"
            android:textSize="20sp"/>
  
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:background="#0F9D58"
            android:layout_margin="5dp"
            android:text="center_horizontal"
            android:textSize="20sp"/>
  
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="right"
            android:background="#bcf5b1"
            android:text="right"
            android:layout_margin="5dp"
            android:textSize="20sp"/>
  
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:background="#0F9D58"
            android:text="center"
            android:layout_margin="5dp"
            android:textSize="20sp"/>
  
    </LinearLayout>
  
</LinearLayout>


Output:

Differences of 2 layouts shown

Differences of 2 layouts shown



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads