Understanding the Difference Between `gravity` and `layout_gravity` in Android

Understanding the Difference Between `gravity` and `layout_gravity` in Android

22 May 2024 Stephan Petzl Leave a comment Tech-Help

When working with Android layouts, you might come across two properties that seem similar at first glance: android:gravity and android:layout_gravity. Although their names are similar, they serve different purposes. This article aims to clarify the difference between these two attributes and provide practical examples to illustrate their use.

Key Differences

The primary difference lies in their scope of influence:

  • android:gravity: 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.
  • android:layout_gravity: This property affects the alignment of the view within its parent. It is used to position the view relative to its parent container.

Practical Examples

Let’s consider a practical example to understand these properties better. Below is an XML layout file demonstrating their usage:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="#e3e2ad"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="24sp"
            android:text="gravity=" />
    
        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="left"
            android:text="left" />
    
        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center_horizontal"
            android:text="center_horizontal" />
    
        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="right"
            android:text="right" />
    
        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center"
            android:text="center" />
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#d6c6cd"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="24sp"
            android:text="layout_gravity=" />
    
        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="left"
            android:background="#bcf5b1"
            android:text="left" />
    
        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:background="#aacaff"
            android:text="center_horizontal" />
    
        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="right"
            android:background="#bcf5b1"
            android:text="right" />
    
        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:background="#aacaff"
            android:text="center" />
    
    </LinearLayout>
    
</LinearLayout>
    

Additional Considerations

  • layout_gravity does not work for views in a RelativeLayout. It is best used in LinearLayout or FrameLayout.
  • The view’s width (or height) must be greater than its content for gravity to have an effect. Therefore, wrap_content and gravity are often meaningless together.
  • The view’s width (or height) must be less than the parent for layout_gravity to have an effect. Thus, match_parent and layout_gravity are often meaningless together.

For more detailed information on how to use these properties, you can refer to our related articles:

Streamlining Mobile App Testing with Repeato

As you continue to develop your Android applications, ensuring thorough testing is crucial. This is where Repeato can be an invaluable tool. Repeato is a no-code test automation tool for iOS and Android that enables you to create, run, and maintain automated tests for your apps efficiently.

By leveraging computer vision and AI, Repeato allows you to focus on developing your product rather than spending excessive time on test creation and maintenance. Additionally, it empowers non-technical colleagues or QA teams to handle test automation, making the entire process more collaborative and streamlined.

Explore more about Repeato and how it can enhance your mobile app development workflow on our documentation page.

Like this article? there’s more where that came from!