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
?
gravity: is used for simple views like textview, edittext etc.
layout_gravity: is used for current view only gravity in context of it's relative parent view like linear Layout or FrameLayout to make view in center or any other gravity of its parent.
Gravity: Allow you move the content inside a container. (How sub-views will be placed).
Important: (MOVE along X-axis or Y-axis within available space).
Example: Let's say if you were to work with LinearLayout (Height: match_parent, Width: match_parent) as root level element, then you will have full frame space available; and the child views says 2 TextViews (Height: wrap_content, Width: wrap_content) inside the LinearLayout can be moved around along x/y axis using corresponding values for gravity on parent.
Layout_Gravity: Allow you to override the parent gravity behavior ONLY along x-axis.
Important: (MOVE[override] along X-axis within available space).
Example: If you keep in mind the previous example, we know gravity enabled us to move along x/y axis, i.e; the place TextViews inside LinearLayout. Let's just say LinearLayout specifies gravity: center; meaning every TextView needs to be center both vertically and horizontally. Now if we want one of the TextView to go left/right, we can override the specified gravity behavior using layout_gravity on the TextView.
Bonus: if you dig deeper, you will find out that text within the TextView act as sub-view; so if you apply the gravity on TextView, the text inside the TextView will move around. (the entire concept apply here too)
Gravity is used to set text alignment in views but layout_gravity is use to set views it self. Lets take an example if you want to align text written in editText then use gravity and you want align this editText or any button or any view then use layout_gravity, So its very simple.
Though the question is already answered I have some samples demonstrating the use of gravity, layout_gravity, and layout_weight.
You can find the examples at http://juanpickselov.com/LayoutExamples.zip
I created the files in Eclipse, removed the .svn subfolders and have included styles, strings, colors, etc. The layout files are the main point of the demos. Since I'm a Java and Android development Newbie, one may find the Java inefficient. The files can be copied into an Eclipse Project or I've also used them in Netbeans with the Android development plugin available for that IDE.
Short Answer: use
android:gravity
orsetGravity()
to control gravity of all child views of a container; useandroid:layout_gravity
orsetLayoutParams()
to control gravity of an individual view in a container.Long story: to control gravity in a linear layout container such as
LinearLayout
orRadioGroup
, there are two approaches:1) To control the gravity of ALL child views of a
LinearLayout
container (as you did in your book), useandroid:gravity
(notandroid:layout_gravity
) in layout XML file orsetGravity()
method in code.2) To control the gravity of a child view in its container, use
android:layout_gravity
XML attribute. In code, one needs to get theLinearLayout.LayoutParams
of the view and set its gravity. Here is a code example that set a button to bottom in a horizontally oriented container:For horizontal
LinearLayout
container, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Settingandroid:layout_gravity
tocenter_horizontal
has no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the defaultlayout_gravity
value is-1
but Android put it center vertically.To change the horizontal positions of child views in a horizontal linear container, one can use
layout_weight
, margin and padding of the child view.Similarly, for vertical View Group container, the vertical gravity of its child view is top-aligned one below another and cannot be changed. The default horizontal gravity is center (or
center_horizontal
) and can be changed to left or right.Actually, a child view such as a button also has
android:gravity
XML attribute and thesetGravity()
method to control its child views -- the text in it. TheButton.setGravity(int)
is linked to this developer.android.com entry.android:gravity
is used to specify how to place the content of the object within the object itself. In another word, android:gravity is used to specify the gravity of the content of the view.android:layout_gravity
is an attribution the child can supply to its parent, to specify the gravity the view within its parents.For more details you can visit