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
?
android:gravity is an attribute that sets the gravity of the content of the view its used on. The android:gravity specifies how an object should position its content on both X and Y axis. The possible values of android:gravity are top, bottom, left, right, center, center_vertical, center_horizontal etc.
So in general android:layout_gravity attribute is used by child views to tell their parent how they want to be placed inside it, while android:gravity is used by the parent layout to tell the child views how they should be placed inside it.
Android provides the following ViewGroups or layouts: LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions.
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding) WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding) an exact number.
Their names should help you:
android:gravity
sets the gravity of the contents (i.e. its subviews) of the View
it's used on. android:layout_gravity
sets the gravity of the View
or Layout
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
.
layout_gravity
does not work for views in a RelativeLayout
. Use it for views in a LinearLayout
or FrameLayout
. See my supplemental answer for more details.gravity
won't have any effect. Thus, wrap_content
and gravity
are meaningless together.layout_gravity
won't have any effect. Thus, match_parent
and layout_gravity
are meaningless together.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.gravity
and layout_gravity
on the views within a layout. To see what happens when you set the gravity
of the of the parent layout itself, check out the supplemental answer that I referred to above. (Summary: gravity
doesn't work well on a RelativeLayout
but can be useful with a LinearLayout
.)So remember, layout_gravity arranges a view in its layout. Gravity arranges the content inside the view.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With