What is the difference between gravity and layout_gravity in Android?
Their names should help you:
-
android:gravity
sets the gravity of the contents (i.e. its subviews) of theView
it's used on. -
android:layout_gravity
sets the gravity of theView
orLayout
relative to its parent.
And an example is here.
Inside - Outside
-
gravity
arranges the content inside the view. -
layout_gravity
arranges the view's position outside of itself.
Sometimes it helps to see a picture, too. The green and blue are TextViews
and the other two background colors are LinearLayouts
.
Notes
- The
layout_gravity
does not work for views in aRelativeLayout
. Use it for views in aLinearLayout
orFrameLayout
. See my supplemental answer for more details. - The view's width (or height) has to be greater than its content. Otherwise
gravity
won't have any effect. Thus,wrap_content
andgravity
are meaningless together. - The view's width (or height) has to be less than the parent. Otherwise
layout_gravity
won't have any effect. Thus,match_parent
andlayout_gravity
are meaningless together. - The
layout_gravity=center
looks the same aslayout_gravity=center_horizontal
here because they are in a vertical linear layout. You can't center vertically in this case, solayout_gravity=center
only centers horizontally. - This answer only dealt with setting
gravity
andlayout_gravity
on the views within a layout. To see what happens when you set thegravity
of the of the parent layout itself, check out the supplemental answer that I referred to above. (Summary:gravity
doesn't work well on aRelativeLayout
but can be useful with aLinearLayout
.)
So remember, layout_gravity arranges a view in its layout. Gravity arranges the content inside the view.
xml
Here is the xml for the above image for your reference:
<?xml version="1.0" encoding="utf-8"?>
<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>
Related
- Difference between a View's Padding and Margin
- Match_parent vs wrap_content
- How to set both gravity and layout gravity of a LinearLayout programatically
The difference
android:layout_gravity
is the Outside gravity of the View. Specifies the direction in which the View should touch its parent's border.
android:gravity
is the Inside gravity of that View. Specifies in which direction its contents should align.
HTML/CSS Equivalents
(if you are coming from a web development background)
Android | CSS
————————————————————————+————————————
android:layout_gravity | float
android:gravity | text-align
Easy trick to help you remember
Take layout-gravity
as "Lay-outside-gravity".