What is the difference between android:gravity and android:layout_gravity in Android?
I know we can set the following values to the android:gravity and android:layout_gravity properties:
- center
- center_vertical
- center_horizontal, etc.
But I am confused regarding both of these. What is the difference between the usage of android:gravity and android:layout_gravity?
The main difference between android:gravity and android:layout_gravity lies in their scope of influence: android:gravity controls the alignment of content inside a view, while android:layout_gravity controls the alignment of a view within its parent container. For example, android:gravity="center" would center text inside a TextView, whereas android:layout_gravity="center" would center the entire TextView within its parent layout.
Contents
- Understanding the Core Difference
- When to Use android:gravity
- When to Use android:layout_gravity
- Practical Examples and Code Snippets
- Layout Compatibility Considerations
- Common Use Cases and Best Practices
Understanding the Core Difference
The fundamental distinction between these two Android layout properties can be understood through a simple analogy:
android:gravity: Controls the position of content inside a view (like how text is positioned within a TextView)android:layout_gravity: Controls the position of the view itself within its parent (like how a TextView is positioned within a LinearLayout)
As GeeksforGeeks explains: “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.”
The Stack Overflow community provides a clear distinction: “Long story: to control gravity … container (as you did in your book), use android:gravity (not android:layout_gravity) in layout XML file or setGravity() method in code.”
When to Use android:gravity
android:gravity is used when you want to control the alignment of content within a specific view. This property tells the view how to arrange its contents (text, child views, etc.) relative to its own boundaries.
Key Use Cases for android:gravity:
- Text alignment within TextView/EditText: Centering, left-aligning, or right-aligning text
- Button content positioning: Aligning text or icons inside a Button
- LinearLayout content control: When using LinearLayout,
android:gravityaffects ALL child views simultaneously - Custom View content: Defining how content should be laid out within your custom view components
According to Repeato: “This property affects the alignment of the content inside the view. It is used to position the contents (such as text or child views) within the view itself.”
When to Use android:layout_gravity
android:layout_gravity is used when you want to control how a specific view is positioned within its parent container. This property acts as a communication channel from child to parent, telling the parent layout how the child should be placed.
Key Use Cases for android:layout_gravity:
- Positioning views in LinearLayout: Centering, left-aligning, or right-aligning a TextView within its parent LinearLayout
- FrameLayout positioning: Placing views at specific edges or center of a FrameLayout
- Custom positioning: When you need a specific child view to have different alignment than others
- Relative positioning: In certain layouts, controlling how a view relates to its parent boundaries
The Medium article explains this well: “android:layout_gravity handles the alignment of itself, it sets the gravity of the View or Layout relative to its parent. In other words it just focuses on the centering of all child views in the parent view.”
Practical Examples and Code Snippets
Let’s explore some concrete examples to better understand the practical application of these properties.
Example 1: Text Centering vs. View Centering
<TextView
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" <!-- Centers TEXT inside the TextView -->
android:layout_gravity="center" <!-- Centers the ENTIRE TextView in parent -->
android:text="Hello World" />
In this example:
android:gravity="center"centers the text “Hello World” within the TextView boundariesandroid:layout_gravity="center"centers the entire TextView within its parent container
Example 2: Multiple Views in LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left aligned text"
android:gravity="start" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right aligned view"
android:layout_gravity="end" />
</LinearLayout>
As shown in Tutorialspoint: “In the above example we kept layout_gravity as right. It going to place at right side of parent view.”
Example 3: LinearLayout with Parent Gravity
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"> <!-- Affects ALL child views -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
</LinearLayout>
According to Stack Overflow: “To control the gravity of ALL child views of a LinearLayout container (as you did in your book), use android:gravity (not android:layout_gravity) in layout XML file or setGravity() method in code.”
Layout Compatibility Considerations
Not all Android layouts support both properties equally. Understanding these limitations is crucial for proper implementation.
LinearLayout Compatibility:
android:gravity: ✅ Supported - controls alignment of all child viewsandroid:layout_gravity: ✅ Supported - controls individual view positioning
FrameLayout Compatibility:
android:gravity: ❌ Not typically usedandroid:layout_gravity: ✅ Supported - excellent for positioning views at edges or center
RelativeLayout Compatibility:
android:gravity: ✅ Supported - for content alignment within viewsandroid:layout_gravity: ❌ Generally not supported - RelativeLayout uses different positioning rules
As GeeksforGeeks notes: “The layout_gravity does not work for views in a RelativeLayout. It is used for views in a LinearLayout or FrameLayout.”
Common Use Cases and Best Practices
Centering Content in a View:
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:gravity="center" <!-- Centers text within TextView -->
android:text="Centered Text" />
Centering a View in Parent:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" <!-- Centers entire TextView in parent -->
android:text="Centered View" />
Complex Layout with Mixed Alignment:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"> <!-- Vertically centers all children -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Center"
android:layout_gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Right"
android:layout_gravity="bottom|end" />
</LinearLayout>
The ProAndroidDev article provides excellent insight: “Thanks to that attribute the child views can somehow communicate with the parent layout and tell how they want to be placed inside it — different layouts respect it in a different way, but in general this is how it’s working. What about communication in the opposite direction — from parent to child views? For that we’ll need android:gravity attribute.”
Conclusion
Understanding the difference between android:gravity and android:layout_gravity is fundamental to mastering Android layout design. Here are the key takeaways:
-
Scope of influence:
android:gravitycontrols content inside a view, whileandroid:layout_gravitycontrols the view’s position within its parent. -
Direction of control:
android:gravityflows from parent to child (parent tells children how to align content), whileandroid:layout_gravityflows from child to parent (child tells parent how to position itself). -
Layout compatibility: Not all layouts support both properties equally - RelativeLayout doesn’t support
android:layout_gravity, while LinearLayout supports both. -
Practical application: Use
android:gravityfor text alignment within views andandroid:layout_gravityfor positioning entire views within their containers.
By mastering these properties, you’ll gain precise control over your Android layouts and create more polished, professional-looking user interfaces. Remember to test your layouts across different screen sizes and orientations to ensure consistent behavior.
Sources
- What is the difference between gravity and layout_gravity in Android? - Stack Overflow
- What is the Difference Between GRAVITY and LAYOUT_GRAVITY in Android? - GeeksforGeeks
- What Is the Difference Between Gravity and Layout_Gravity in Android? – Learn to Droid
- Understanding the Difference Between
gravityandlayout_gravityin Android - Repeato - What is difference between gravity and layout_gravity on Android? - Tutorialspoint
- Understanding the difference between gravity and layout_gravity | en.proft.me
- Difference between android:layout_gravity and android:gravity | Medium
- Android Developer Beginner. FAQ #2 — gravity vs layout_gravity | ProAndroidDev
- Android Gravity - Tutorialride
- Android Tutorial => Gravity and layout gravity - Riptutorial