What is the difference between gravity and layout_gravity in Android?










1228















I know we can set the following values to the android:gravity and android:layout_gravity properties:



  1. center

  2. center_vertical


  3. 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?










share|improve this question



















  • 246





    Easy trick to remember: Take "layout-gravity" as "Lay-outside-gravity"

    – Vishnu Haridas
    Dec 30 '11 at 9:22






  • 6





    center == center_vertical | center_horizontal

    – Yousha Aleayoub
    Sep 16 '16 at 14:33






  • 3





    These videos helped me a lot to understand the difference: youtube.com/watch?v=DxfYeAUd238 youtube.com/watch?v=1FyAIWLVcTc

    – Roni Castro
    Oct 21 '16 at 5:16











  • ImageView's seem to behave different than TextView's.

    – samis
    Aug 16 '17 at 16:42















1228















I know we can set the following values to the android:gravity and android:layout_gravity properties:



  1. center

  2. center_vertical


  3. 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?










share|improve this question



















  • 246





    Easy trick to remember: Take "layout-gravity" as "Lay-outside-gravity"

    – Vishnu Haridas
    Dec 30 '11 at 9:22






  • 6





    center == center_vertical | center_horizontal

    – Yousha Aleayoub
    Sep 16 '16 at 14:33






  • 3





    These videos helped me a lot to understand the difference: youtube.com/watch?v=DxfYeAUd238 youtube.com/watch?v=1FyAIWLVcTc

    – Roni Castro
    Oct 21 '16 at 5:16











  • ImageView's seem to behave different than TextView's.

    – samis
    Aug 16 '17 at 16:42













1228












1228








1228


327






I know we can set the following values to the android:gravity and android:layout_gravity properties:



  1. center

  2. center_vertical


  3. 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?










share|improve this question
















I know we can set the following values to the android:gravity and android:layout_gravity properties:



  1. center

  2. center_vertical


  3. 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 android-layout android-gravity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 12 at 8:57









Bernd Wilke πφ

5,2241825




5,2241825










asked Aug 14 '10 at 9:28









Paresh MayaniParesh Mayani

97.1k62223282




97.1k62223282







  • 246





    Easy trick to remember: Take "layout-gravity" as "Lay-outside-gravity"

    – Vishnu Haridas
    Dec 30 '11 at 9:22






  • 6





    center == center_vertical | center_horizontal

    – Yousha Aleayoub
    Sep 16 '16 at 14:33






  • 3





    These videos helped me a lot to understand the difference: youtube.com/watch?v=DxfYeAUd238 youtube.com/watch?v=1FyAIWLVcTc

    – Roni Castro
    Oct 21 '16 at 5:16











  • ImageView's seem to behave different than TextView's.

    – samis
    Aug 16 '17 at 16:42












  • 246





    Easy trick to remember: Take "layout-gravity" as "Lay-outside-gravity"

    – Vishnu Haridas
    Dec 30 '11 at 9:22






  • 6





    center == center_vertical | center_horizontal

    – Yousha Aleayoub
    Sep 16 '16 at 14:33






  • 3





    These videos helped me a lot to understand the difference: youtube.com/watch?v=DxfYeAUd238 youtube.com/watch?v=1FyAIWLVcTc

    – Roni Castro
    Oct 21 '16 at 5:16











  • ImageView's seem to behave different than TextView's.

    – samis
    Aug 16 '17 at 16:42







246




246





Easy trick to remember: Take "layout-gravity" as "Lay-outside-gravity"

– Vishnu Haridas
Dec 30 '11 at 9:22





Easy trick to remember: Take "layout-gravity" as "Lay-outside-gravity"

– Vishnu Haridas
Dec 30 '11 at 9:22




6




6





center == center_vertical | center_horizontal

– Yousha Aleayoub
Sep 16 '16 at 14:33





center == center_vertical | center_horizontal

– Yousha Aleayoub
Sep 16 '16 at 14:33




3




3





These videos helped me a lot to understand the difference: youtube.com/watch?v=DxfYeAUd238 youtube.com/watch?v=1FyAIWLVcTc

– Roni Castro
Oct 21 '16 at 5:16





These videos helped me a lot to understand the difference: youtube.com/watch?v=DxfYeAUd238 youtube.com/watch?v=1FyAIWLVcTc

– Roni Castro
Oct 21 '16 at 5:16













ImageView's seem to behave different than TextView's.

– samis
Aug 16 '17 at 16:42





ImageView's seem to behave different than TextView's.

– samis
Aug 16 '17 at 16:42












20 Answers
20






active

oldest

votes


















1264














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.






share|improve this answer




















  • 77





    Basically, everything with layout_ defines something that effects the elements outside.

    – Triang3l
    Jun 28 '13 at 10:26






  • 23





    See, I find this funny, because if I just go off the names, my intuition is the other way around. Every time, I think "layout_gravity" means "the gravity for how this ViewGroup lays out it's contents", and "gravity" is "where this View gravitates to".

    – Ogre
    Aug 9 '13 at 5:29






  • 13





    So what happens if the android:gravity is set to left and its children's android:layout_gravity is set to right? which side will the children align to?

    – Thupten
    Mar 28 '14 at 20:52






  • 2





    As @Suragch commented on his answer, layout_gravity only works in LinearLayout and FrameLayout. LinearLayout has constraints to it though.

    – Marco Luglio
    Jun 25 '15 at 5:49











  • @Thupten Very good question. I guess one will prevail (I guess the children's layout_gravity) and the other one is only the default for the prevailing one.

    – Trilarion
    Dec 28 '15 at 9:30


















465














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.



enter image description here



Notes



  • The 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.

  • The view's width (or height) has to be greater than its content. Otherwise gravity won't have any effect. Thus, wrap_content and gravity 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 and layout_gravity are meaningless together.

  • The 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.

  • This answer only dealt with setting 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.



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





share|improve this answer
































    452














    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



    Android | CSS
    ————————————————————————+————————————
    android:layout_gravity | float
    android:gravity | text-align


    Easy trick to help you remember



    Take layout-gravity as "Lay-outside-gravity".






    share|improve this answer
































      37














      Short Answer: use android:gravity or setGravity() to control gravity of all child views of a container; use android:layout_gravity or setLayoutParams() to control gravity of an individual view in a container.



      Long story: to control gravity in a linear layout container such as LinearLayout or RadioGroup, there are two approaches:



      1) 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.



      2) To control the gravity of a child view in its container, use android:layout_gravity XML attribute. In code, one needs to get the LinearLayout.LayoutParams of the view and set its gravity. Here is a code example that set a button to bottom in a horizontally oriented container:



      import android.widget.LinearLayout.LayoutParams;
      import android.view.Gravity;
      ...

      Button button = (Button) findViewById(R.id.MyButtonId);
      // need to cast to LinearLayout.LayoutParams to access the gravity field
      LayoutParams params = (LayoutParams)button.getLayoutParams();
      params.gravity = Gravity.BOTTOM;
      button.setLayoutParams(params);


      For horizontal LinearLayout container, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Setting android:layout_gravity to center_horizontal has no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the default layout_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 the setGravity() method to control its child views -- the text in it. The Button.setGravity(int) is linked to this developer.android.com entry.






      share|improve this answer
































        33














        From what I can gather layout_gravity is the gravity of that view inside its parent, and gravity is the gravity of the children inside that view.



        I think this is right but the best way to find out is to play around.






        share|improve this answer






























          24
















          Look at the image to be clear about gravity






          share|improve this answer






























            12














            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.






            share|improve this answer


















            • 2





              Somehow, two and a half years later, the example link is resulting in a 404.

              – Tony Adams
              Jun 14 '13 at 18:57


















            10














            If a we want to set the gravity of content inside a view then we will use "android:gravity", and if we want to set the gravity of this view (as a whole) within its parent view then we will use "android:layout_gravity".






            share|improve this answer
































              7














              Just thought I'd add my own explanation here - coming from a background on iOS, this is how I've internalized the two in iOS terms: "Layout Gravity" affects your position in the superview. "Gravity" affects the position of your subviews within you. Said another way, Layout Gravity positions you yourself while gravity positions your children.






              share|improve this answer






























                6














                There is many difference in the gravity and layout-gravity. I am going to explain my experience about these 2 concepts(All information i got due to my observation and some websites).




                Use Of Gravity and Layout-gravity in FrameLayout .....




                Note:-



                1. Gravity is used inside the View Content as some User have answer and it is same for all ViewGroup Layout.


                2. Layout-gravity is used with the parent View as some User have answer.


                3. Gravity and Layout-gravity is work more useful with the FrameLayout childs . We can't use Gravity and Layout-gravity in FrameLayout's Tag ....


                4. We can set Child View any where in the FrameLayout using layout-gravity .


                5. We can use every single value of gravity inside the FrameLayout (eg:- center_vertical, center_horizontal, center,top, etc), but it is not possible with other ViewGroup Layouts .


                6. FrameLayout fully working on Layout-gravity. Example:- If you work on FrameLayout then you don't need to change whole Layout for adding new View. You just add View as last in the FrameLayout and give him Layout-gravity with value.(This is adavantages of layout-gravity with FrameLayout).



                have look on example ......




                <?xml version="1.0" encoding="utf-8"?>
                <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:textSize="25dp"
                android:background="#000"
                android:textColor="#264bd1"
                android:gravity="center"
                android:layout_gravity="center"
                android:text="Center Layout Gravity"/>

                <TextView
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:textSize="25dp"
                android:background="#000"
                android:textColor="#1b64b9"
                android:gravity="bottom"
                android:layout_gravity="bottom|center"
                android:text="Bottom Layout Gravity" />

                <TextView
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:textSize="25dp"
                android:background="#000"
                android:textColor="#d75d1c"
                android:gravity="top"
                android:layout_gravity="top|center"
                android:text="Top Layout Gravity"/>

                <TextView
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:textSize="25dp"
                android:background="#000"
                android:layout_marginTop="100dp"
                android:textColor="#d71f1c"
                android:gravity="top|right"
                android:layout_gravity="top|right"
                android:text="Top Right Layout Gravity"/>


                <TextView
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:textSize="25dp"
                android:background="#000"
                android:layout_marginBottom="100dp"
                android:textColor="#d71cb2"
                android:layout_gravity="bottom"
                android:gravity="bottom"
                android:text="Top Left Layout Gravity"/>

                </FrameLayout>


                Output:-



                g1




                Use Of Gravity and Layout-gravity in LinearLayout .....




                Gravity working same as above but here differnce is that we can use Gravity inside the LinearLayout View and RelativeLayout View which is not possible in FrameLayout View.




                LinearLayout with orientation vertical ....




                Note:- Here we can set only 3 values of layout_gravity that is (left | right | center (also called center_horizontal)).




                have look on example :-




                <?xml version="1.0" encoding="utf-8"?>
                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="match_parent">

                <TextView
                android:layout_width="wrap_content"
                android:layout_height="100dp"
                android:textSize="25dp"
                android:background="#000"
                android:textColor="#264bd1"
                android:gravity="center"
                android:layout_gravity="center_horizontal"
                android:text="Center Layout Gravity nor nCenter_Horizontal"/>

                <TextView
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:textSize="25dp"
                android:background="#000"
                android:layout_marginTop="20dp"
                android:textColor="#d75d1c"
                android:layout_gravity="right"
                android:text="Right Layout Gravity"/>


                <TextView
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:textSize="25dp"
                android:background="#000"
                android:layout_marginBottom="100dp"
                android:textColor="#d71cb2"
                android:layout_gravity="left"
                android:layout_marginTop="20dp"
                android:gravity="bottom"
                android:text="Left Layout Gravity"/>

                </LinearLayout>


                Output:-



                g2




                LinearLayout with orientation horizontal ....




                Note:- Here we can set also 3 values of layout_gravity that is (top | bottom | center (also called center_vertical)).




                have look on example :-




                <?xml version="1.0" encoding="utf-8"?>
                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_height="match_parent">

                <TextView
                android:layout_width="120dp"
                android:layout_height="100dp"
                android:textSize="25dp"
                android:background="#000"
                android:textColor="#264bd1"
                android:gravity="center"
                android:layout_gravity="bottom"
                android:text="Bottom nLayout nGravity"/>

                <TextView
                android:layout_width="120dp"
                android:layout_height="100dp"
                android:textSize="25dp"
                android:background="#000"
                android:layout_marginTop="20dp"
                android:textColor="#d75d1c"
                android:layout_gravity="center"
                android:text="Center nLayout nGravity"/>


                <TextView
                android:layout_width="150dp"
                android:layout_height="100dp"
                android:textSize="25dp"
                android:background="#000"
                android:layout_marginBottom="100dp"
                android:textColor="#d71cb2"
                android:layout_gravity="left"
                android:layout_marginTop="20dp"
                android:text="Left nLayout nGravity"/>

                </LinearLayout>


                output:-



                g3



                Note:- We can't use layout_gravity in the RelativeLayout Views but we can use gravity to set RelativeLayout childs to same position....






                share|improve this answer


















                • 1





                  If you set textColor white in all view then it would be very great. :D

                  – Pratik Butani AndroidDev
                  Feb 6 at 8:47











                • Are you sure? It will look black and white that's why i used different color for every View.

                  – Sushil Kumar
                  Feb 6 at 9:54


















                4














                Something I saw on Sandip's blog that I almost missed, fixed my problem. He said layout_gravity DOES NOT WORK WITH LinearLayout.



                If you're using a LinearLayout and the gravity settings are driving you nuts (like me), then switch to something else.



                I actually switched to a RelativeLayout then used layout_alignParentLeft and layout_alignParentRight on the 2 contained TextViews to get them on one line to go far left and far right.






                share|improve this answer
































                  4














                  An easy trick to remember this is gravity applies to us inside earth. So, android:gravity is for inside the view.



                  Rememeber the out in layout_gravity which would help you to remember that android:layout_gravity would refer to outside the view






                  share|improve this answer
































                    2














                    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




                    http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html







                    share|improve this answer


















                    • 2





                      I think no need of new answer as there are multiple answers already posted and with almost the same contents covered!

                      – Paresh Mayani
                      May 10 '15 at 13:56


















                    2














                    The basic difference between the two is that-



                    android:gravity is used for child elements of the view.



                    android:layout_gravity is used for this element with respect to parent view.






                    share|improve this answer
































                      2














                      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)






                      share|improve this answer






























                        1














                        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.






                        share|improve this answer






























                          1














                          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.






                          share|improve this answer

























                          • layout_gravity: is used for current view only gravity in context of it's relative parent, not other views which is inside of it's parent.

                            – Chintak Patel
                            Feb 7 '18 at 10:12


















                          0














                          android:gravity


                          is used to adjust for content of the view relative to its specify position (allocated area). android:gravity="left" would not do anything if layout_width is equal to the "wrap_content"



                          android:layout_gravity 


                          is used for view itself relative to the parent or layout file.






                          share|improve this answer






























                            0














                            android:gravity -> Sets the gravity of the content of the View its used on.



                            android:layout_gravity -> Sets the gravity of it's Parent's view or Layout






                            share|improve this answer






























                              0














                              gravity--Applies to its own view.



                              layout-gravity---Applies to view related to its parent.






                              share|improve this answer





















                                protected by Community May 18 '13 at 4:07



                                Thank you for your interest in this question.
                                Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                Would you like to answer one of these unanswered questions instead?














                                20 Answers
                                20






                                active

                                oldest

                                votes








                                20 Answers
                                20






                                active

                                oldest

                                votes









                                active

                                oldest

                                votes






                                active

                                oldest

                                votes









                                1264














                                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.






                                share|improve this answer




















                                • 77





                                  Basically, everything with layout_ defines something that effects the elements outside.

                                  – Triang3l
                                  Jun 28 '13 at 10:26






                                • 23





                                  See, I find this funny, because if I just go off the names, my intuition is the other way around. Every time, I think "layout_gravity" means "the gravity for how this ViewGroup lays out it's contents", and "gravity" is "where this View gravitates to".

                                  – Ogre
                                  Aug 9 '13 at 5:29






                                • 13





                                  So what happens if the android:gravity is set to left and its children's android:layout_gravity is set to right? which side will the children align to?

                                  – Thupten
                                  Mar 28 '14 at 20:52






                                • 2





                                  As @Suragch commented on his answer, layout_gravity only works in LinearLayout and FrameLayout. LinearLayout has constraints to it though.

                                  – Marco Luglio
                                  Jun 25 '15 at 5:49











                                • @Thupten Very good question. I guess one will prevail (I guess the children's layout_gravity) and the other one is only the default for the prevailing one.

                                  – Trilarion
                                  Dec 28 '15 at 9:30















                                1264














                                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.






                                share|improve this answer




















                                • 77





                                  Basically, everything with layout_ defines something that effects the elements outside.

                                  – Triang3l
                                  Jun 28 '13 at 10:26






                                • 23





                                  See, I find this funny, because if I just go off the names, my intuition is the other way around. Every time, I think "layout_gravity" means "the gravity for how this ViewGroup lays out it's contents", and "gravity" is "where this View gravitates to".

                                  – Ogre
                                  Aug 9 '13 at 5:29






                                • 13





                                  So what happens if the android:gravity is set to left and its children's android:layout_gravity is set to right? which side will the children align to?

                                  – Thupten
                                  Mar 28 '14 at 20:52






                                • 2





                                  As @Suragch commented on his answer, layout_gravity only works in LinearLayout and FrameLayout. LinearLayout has constraints to it though.

                                  – Marco Luglio
                                  Jun 25 '15 at 5:49











                                • @Thupten Very good question. I guess one will prevail (I guess the children's layout_gravity) and the other one is only the default for the prevailing one.

                                  – Trilarion
                                  Dec 28 '15 at 9:30













                                1264












                                1264








                                1264







                                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.






                                share|improve this answer















                                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.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Feb 8 at 21:28









                                danronmoon

                                2,84042549




                                2,84042549










                                answered Aug 14 '10 at 9:31









                                SephySephy

                                38k27107122




                                38k27107122







                                • 77





                                  Basically, everything with layout_ defines something that effects the elements outside.

                                  – Triang3l
                                  Jun 28 '13 at 10:26






                                • 23





                                  See, I find this funny, because if I just go off the names, my intuition is the other way around. Every time, I think "layout_gravity" means "the gravity for how this ViewGroup lays out it's contents", and "gravity" is "where this View gravitates to".

                                  – Ogre
                                  Aug 9 '13 at 5:29






                                • 13





                                  So what happens if the android:gravity is set to left and its children's android:layout_gravity is set to right? which side will the children align to?

                                  – Thupten
                                  Mar 28 '14 at 20:52






                                • 2





                                  As @Suragch commented on his answer, layout_gravity only works in LinearLayout and FrameLayout. LinearLayout has constraints to it though.

                                  – Marco Luglio
                                  Jun 25 '15 at 5:49











                                • @Thupten Very good question. I guess one will prevail (I guess the children's layout_gravity) and the other one is only the default for the prevailing one.

                                  – Trilarion
                                  Dec 28 '15 at 9:30












                                • 77





                                  Basically, everything with layout_ defines something that effects the elements outside.

                                  – Triang3l
                                  Jun 28 '13 at 10:26






                                • 23





                                  See, I find this funny, because if I just go off the names, my intuition is the other way around. Every time, I think "layout_gravity" means "the gravity for how this ViewGroup lays out it's contents", and "gravity" is "where this View gravitates to".

                                  – Ogre
                                  Aug 9 '13 at 5:29






                                • 13





                                  So what happens if the android:gravity is set to left and its children's android:layout_gravity is set to right? which side will the children align to?

                                  – Thupten
                                  Mar 28 '14 at 20:52






                                • 2





                                  As @Suragch commented on his answer, layout_gravity only works in LinearLayout and FrameLayout. LinearLayout has constraints to it though.

                                  – Marco Luglio
                                  Jun 25 '15 at 5:49











                                • @Thupten Very good question. I guess one will prevail (I guess the children's layout_gravity) and the other one is only the default for the prevailing one.

                                  – Trilarion
                                  Dec 28 '15 at 9:30







                                77




                                77





                                Basically, everything with layout_ defines something that effects the elements outside.

                                – Triang3l
                                Jun 28 '13 at 10:26





                                Basically, everything with layout_ defines something that effects the elements outside.

                                – Triang3l
                                Jun 28 '13 at 10:26




                                23




                                23





                                See, I find this funny, because if I just go off the names, my intuition is the other way around. Every time, I think "layout_gravity" means "the gravity for how this ViewGroup lays out it's contents", and "gravity" is "where this View gravitates to".

                                – Ogre
                                Aug 9 '13 at 5:29





                                See, I find this funny, because if I just go off the names, my intuition is the other way around. Every time, I think "layout_gravity" means "the gravity for how this ViewGroup lays out it's contents", and "gravity" is "where this View gravitates to".

                                – Ogre
                                Aug 9 '13 at 5:29




                                13




                                13





                                So what happens if the android:gravity is set to left and its children's android:layout_gravity is set to right? which side will the children align to?

                                – Thupten
                                Mar 28 '14 at 20:52





                                So what happens if the android:gravity is set to left and its children's android:layout_gravity is set to right? which side will the children align to?

                                – Thupten
                                Mar 28 '14 at 20:52




                                2




                                2





                                As @Suragch commented on his answer, layout_gravity only works in LinearLayout and FrameLayout. LinearLayout has constraints to it though.

                                – Marco Luglio
                                Jun 25 '15 at 5:49





                                As @Suragch commented on his answer, layout_gravity only works in LinearLayout and FrameLayout. LinearLayout has constraints to it though.

                                – Marco Luglio
                                Jun 25 '15 at 5:49













                                @Thupten Very good question. I guess one will prevail (I guess the children's layout_gravity) and the other one is only the default for the prevailing one.

                                – Trilarion
                                Dec 28 '15 at 9:30





                                @Thupten Very good question. I guess one will prevail (I guess the children's layout_gravity) and the other one is only the default for the prevailing one.

                                – Trilarion
                                Dec 28 '15 at 9:30













                                465














                                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.



                                enter image description here



                                Notes



                                • The 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.

                                • The view's width (or height) has to be greater than its content. Otherwise gravity won't have any effect. Thus, wrap_content and gravity 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 and layout_gravity are meaningless together.

                                • The 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.

                                • This answer only dealt with setting 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.



                                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





                                share|improve this answer





























                                  465














                                  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.



                                  enter image description here



                                  Notes



                                  • The 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.

                                  • The view's width (or height) has to be greater than its content. Otherwise gravity won't have any effect. Thus, wrap_content and gravity 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 and layout_gravity are meaningless together.

                                  • The 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.

                                  • This answer only dealt with setting 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.



                                  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





                                  share|improve this answer



























                                    465












                                    465








                                    465







                                    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.



                                    enter image description here



                                    Notes



                                    • The 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.

                                    • The view's width (or height) has to be greater than its content. Otherwise gravity won't have any effect. Thus, wrap_content and gravity 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 and layout_gravity are meaningless together.

                                    • The 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.

                                    • This answer only dealt with setting 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.



                                    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





                                    share|improve this answer















                                    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.



                                    enter image description here



                                    Notes



                                    • The 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.

                                    • The view's width (or height) has to be greater than its content. Otherwise gravity won't have any effect. Thus, wrap_content and gravity 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 and layout_gravity are meaningless together.

                                    • The 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.

                                    • This answer only dealt with setting 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.



                                    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






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited May 23 '17 at 12:10









                                    Community

                                    11




                                    11










                                    answered Oct 4 '14 at 5:43









                                    SuragchSuragch

                                    211k125709773




                                    211k125709773





















                                        452














                                        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



                                        Android | CSS
                                        ————————————————————————+————————————
                                        android:layout_gravity | float
                                        android:gravity | text-align


                                        Easy trick to help you remember



                                        Take layout-gravity as "Lay-outside-gravity".






                                        share|improve this answer





























                                          452














                                          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



                                          Android | CSS
                                          ————————————————————————+————————————
                                          android:layout_gravity | float
                                          android:gravity | text-align


                                          Easy trick to help you remember



                                          Take layout-gravity as "Lay-outside-gravity".






                                          share|improve this answer



























                                            452












                                            452








                                            452







                                            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



                                            Android | CSS
                                            ————————————————————————+————————————
                                            android:layout_gravity | float
                                            android:gravity | text-align


                                            Easy trick to help you remember



                                            Take layout-gravity as "Lay-outside-gravity".






                                            share|improve this answer















                                            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



                                            Android | CSS
                                            ————————————————————————+————————————
                                            android:layout_gravity | float
                                            android:gravity | text-align


                                            Easy trick to help you remember



                                            Take layout-gravity as "Lay-outside-gravity".







                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Feb 5 '18 at 2:46









                                            Cœur

                                            19.1k9114155




                                            19.1k9114155










                                            answered Jul 25 '11 at 17:07









                                            Vishnu HaridasVishnu Haridas

                                            6,10621833




                                            6,10621833





















                                                37














                                                Short Answer: use android:gravity or setGravity() to control gravity of all child views of a container; use android:layout_gravity or setLayoutParams() to control gravity of an individual view in a container.



                                                Long story: to control gravity in a linear layout container such as LinearLayout or RadioGroup, there are two approaches:



                                                1) 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.



                                                2) To control the gravity of a child view in its container, use android:layout_gravity XML attribute. In code, one needs to get the LinearLayout.LayoutParams of the view and set its gravity. Here is a code example that set a button to bottom in a horizontally oriented container:



                                                import android.widget.LinearLayout.LayoutParams;
                                                import android.view.Gravity;
                                                ...

                                                Button button = (Button) findViewById(R.id.MyButtonId);
                                                // need to cast to LinearLayout.LayoutParams to access the gravity field
                                                LayoutParams params = (LayoutParams)button.getLayoutParams();
                                                params.gravity = Gravity.BOTTOM;
                                                button.setLayoutParams(params);


                                                For horizontal LinearLayout container, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Setting android:layout_gravity to center_horizontal has no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the default layout_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 the setGravity() method to control its child views -- the text in it. The Button.setGravity(int) is linked to this developer.android.com entry.






                                                share|improve this answer





























                                                  37














                                                  Short Answer: use android:gravity or setGravity() to control gravity of all child views of a container; use android:layout_gravity or setLayoutParams() to control gravity of an individual view in a container.



                                                  Long story: to control gravity in a linear layout container such as LinearLayout or RadioGroup, there are two approaches:



                                                  1) 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.



                                                  2) To control the gravity of a child view in its container, use android:layout_gravity XML attribute. In code, one needs to get the LinearLayout.LayoutParams of the view and set its gravity. Here is a code example that set a button to bottom in a horizontally oriented container:



                                                  import android.widget.LinearLayout.LayoutParams;
                                                  import android.view.Gravity;
                                                  ...

                                                  Button button = (Button) findViewById(R.id.MyButtonId);
                                                  // need to cast to LinearLayout.LayoutParams to access the gravity field
                                                  LayoutParams params = (LayoutParams)button.getLayoutParams();
                                                  params.gravity = Gravity.BOTTOM;
                                                  button.setLayoutParams(params);


                                                  For horizontal LinearLayout container, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Setting android:layout_gravity to center_horizontal has no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the default layout_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 the setGravity() method to control its child views -- the text in it. The Button.setGravity(int) is linked to this developer.android.com entry.






                                                  share|improve this answer



























                                                    37












                                                    37








                                                    37







                                                    Short Answer: use android:gravity or setGravity() to control gravity of all child views of a container; use android:layout_gravity or setLayoutParams() to control gravity of an individual view in a container.



                                                    Long story: to control gravity in a linear layout container such as LinearLayout or RadioGroup, there are two approaches:



                                                    1) 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.



                                                    2) To control the gravity of a child view in its container, use android:layout_gravity XML attribute. In code, one needs to get the LinearLayout.LayoutParams of the view and set its gravity. Here is a code example that set a button to bottom in a horizontally oriented container:



                                                    import android.widget.LinearLayout.LayoutParams;
                                                    import android.view.Gravity;
                                                    ...

                                                    Button button = (Button) findViewById(R.id.MyButtonId);
                                                    // need to cast to LinearLayout.LayoutParams to access the gravity field
                                                    LayoutParams params = (LayoutParams)button.getLayoutParams();
                                                    params.gravity = Gravity.BOTTOM;
                                                    button.setLayoutParams(params);


                                                    For horizontal LinearLayout container, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Setting android:layout_gravity to center_horizontal has no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the default layout_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 the setGravity() method to control its child views -- the text in it. The Button.setGravity(int) is linked to this developer.android.com entry.






                                                    share|improve this answer















                                                    Short Answer: use android:gravity or setGravity() to control gravity of all child views of a container; use android:layout_gravity or setLayoutParams() to control gravity of an individual view in a container.



                                                    Long story: to control gravity in a linear layout container such as LinearLayout or RadioGroup, there are two approaches:



                                                    1) 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.



                                                    2) To control the gravity of a child view in its container, use android:layout_gravity XML attribute. In code, one needs to get the LinearLayout.LayoutParams of the view and set its gravity. Here is a code example that set a button to bottom in a horizontally oriented container:



                                                    import android.widget.LinearLayout.LayoutParams;
                                                    import android.view.Gravity;
                                                    ...

                                                    Button button = (Button) findViewById(R.id.MyButtonId);
                                                    // need to cast to LinearLayout.LayoutParams to access the gravity field
                                                    LayoutParams params = (LayoutParams)button.getLayoutParams();
                                                    params.gravity = Gravity.BOTTOM;
                                                    button.setLayoutParams(params);


                                                    For horizontal LinearLayout container, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Setting android:layout_gravity to center_horizontal has no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the default layout_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 the setGravity() method to control its child views -- the text in it. The Button.setGravity(int) is linked to this developer.android.com entry.







                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Sep 9 '12 at 1:20









                                                    Mike Mackintosh

                                                    11.5k64675




                                                    11.5k64675










                                                    answered Jul 30 '11 at 20:01









                                                    YingYing

                                                    1,9271918




                                                    1,9271918





















                                                        33














                                                        From what I can gather layout_gravity is the gravity of that view inside its parent, and gravity is the gravity of the children inside that view.



                                                        I think this is right but the best way to find out is to play around.






                                                        share|improve this answer



























                                                          33














                                                          From what I can gather layout_gravity is the gravity of that view inside its parent, and gravity is the gravity of the children inside that view.



                                                          I think this is right but the best way to find out is to play around.






                                                          share|improve this answer

























                                                            33












                                                            33








                                                            33







                                                            From what I can gather layout_gravity is the gravity of that view inside its parent, and gravity is the gravity of the children inside that view.



                                                            I think this is right but the best way to find out is to play around.






                                                            share|improve this answer













                                                            From what I can gather layout_gravity is the gravity of that view inside its parent, and gravity is the gravity of the children inside that view.



                                                            I think this is right but the best way to find out is to play around.







                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Aug 14 '10 at 9:37









                                                            matto1990matto1990

                                                            2,30821824




                                                            2,30821824





















                                                                24
















                                                                Look at the image to be clear about gravity






                                                                share|improve this answer



























                                                                  24
















                                                                  Look at the image to be clear about gravity






                                                                  share|improve this answer

























                                                                    24












                                                                    24








                                                                    24









                                                                    Look at the image to be clear about gravity






                                                                    share|improve this answer















                                                                    Look at the image to be clear about gravity







                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Feb 4 '15 at 7:17









                                                                    Akshay PaliwalAkshay Paliwal

                                                                    2,21912537




                                                                    2,21912537





















                                                                        12














                                                                        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.






                                                                        share|improve this answer


















                                                                        • 2





                                                                          Somehow, two and a half years later, the example link is resulting in a 404.

                                                                          – Tony Adams
                                                                          Jun 14 '13 at 18:57















                                                                        12














                                                                        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.






                                                                        share|improve this answer


















                                                                        • 2





                                                                          Somehow, two and a half years later, the example link is resulting in a 404.

                                                                          – Tony Adams
                                                                          Jun 14 '13 at 18:57













                                                                        12












                                                                        12








                                                                        12







                                                                        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.






                                                                        share|improve this answer













                                                                        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.







                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Nov 23 '10 at 5:43







                                                                        user244298














                                                                        • 2





                                                                          Somehow, two and a half years later, the example link is resulting in a 404.

                                                                          – Tony Adams
                                                                          Jun 14 '13 at 18:57












                                                                        • 2





                                                                          Somehow, two and a half years later, the example link is resulting in a 404.

                                                                          – Tony Adams
                                                                          Jun 14 '13 at 18:57







                                                                        2




                                                                        2





                                                                        Somehow, two and a half years later, the example link is resulting in a 404.

                                                                        – Tony Adams
                                                                        Jun 14 '13 at 18:57





                                                                        Somehow, two and a half years later, the example link is resulting in a 404.

                                                                        – Tony Adams
                                                                        Jun 14 '13 at 18:57











                                                                        10














                                                                        If a we want to set the gravity of content inside a view then we will use "android:gravity", and if we want to set the gravity of this view (as a whole) within its parent view then we will use "android:layout_gravity".






                                                                        share|improve this answer





























                                                                          10














                                                                          If a we want to set the gravity of content inside a view then we will use "android:gravity", and if we want to set the gravity of this view (as a whole) within its parent view then we will use "android:layout_gravity".






                                                                          share|improve this answer



























                                                                            10












                                                                            10








                                                                            10







                                                                            If a we want to set the gravity of content inside a view then we will use "android:gravity", and if we want to set the gravity of this view (as a whole) within its parent view then we will use "android:layout_gravity".






                                                                            share|improve this answer















                                                                            If a we want to set the gravity of content inside a view then we will use "android:gravity", and if we want to set the gravity of this view (as a whole) within its parent view then we will use "android:layout_gravity".







                                                                            share|improve this answer














                                                                            share|improve this answer



                                                                            share|improve this answer








                                                                            edited Apr 28 '13 at 16:45









                                                                            Artychoke

                                                                            312




                                                                            312










                                                                            answered Apr 9 '13 at 21:21









                                                                            Abhishek TamtaAbhishek Tamta

                                                                            399414




                                                                            399414





















                                                                                7














                                                                                Just thought I'd add my own explanation here - coming from a background on iOS, this is how I've internalized the two in iOS terms: "Layout Gravity" affects your position in the superview. "Gravity" affects the position of your subviews within you. Said another way, Layout Gravity positions you yourself while gravity positions your children.






                                                                                share|improve this answer



























                                                                                  7














                                                                                  Just thought I'd add my own explanation here - coming from a background on iOS, this is how I've internalized the two in iOS terms: "Layout Gravity" affects your position in the superview. "Gravity" affects the position of your subviews within you. Said another way, Layout Gravity positions you yourself while gravity positions your children.






                                                                                  share|improve this answer

























                                                                                    7












                                                                                    7








                                                                                    7







                                                                                    Just thought I'd add my own explanation here - coming from a background on iOS, this is how I've internalized the two in iOS terms: "Layout Gravity" affects your position in the superview. "Gravity" affects the position of your subviews within you. Said another way, Layout Gravity positions you yourself while gravity positions your children.






                                                                                    share|improve this answer













                                                                                    Just thought I'd add my own explanation here - coming from a background on iOS, this is how I've internalized the two in iOS terms: "Layout Gravity" affects your position in the superview. "Gravity" affects the position of your subviews within you. Said another way, Layout Gravity positions you yourself while gravity positions your children.







                                                                                    share|improve this answer












                                                                                    share|improve this answer



                                                                                    share|improve this answer










                                                                                    answered Apr 15 '15 at 20:56









                                                                                    mszaromszaro

                                                                                    1,07211025




                                                                                    1,07211025





















                                                                                        6














                                                                                        There is many difference in the gravity and layout-gravity. I am going to explain my experience about these 2 concepts(All information i got due to my observation and some websites).




                                                                                        Use Of Gravity and Layout-gravity in FrameLayout .....




                                                                                        Note:-



                                                                                        1. Gravity is used inside the View Content as some User have answer and it is same for all ViewGroup Layout.


                                                                                        2. Layout-gravity is used with the parent View as some User have answer.


                                                                                        3. Gravity and Layout-gravity is work more useful with the FrameLayout childs . We can't use Gravity and Layout-gravity in FrameLayout's Tag ....


                                                                                        4. We can set Child View any where in the FrameLayout using layout-gravity .


                                                                                        5. We can use every single value of gravity inside the FrameLayout (eg:- center_vertical, center_horizontal, center,top, etc), but it is not possible with other ViewGroup Layouts .


                                                                                        6. FrameLayout fully working on Layout-gravity. Example:- If you work on FrameLayout then you don't need to change whole Layout for adding new View. You just add View as last in the FrameLayout and give him Layout-gravity with value.(This is adavantages of layout-gravity with FrameLayout).



                                                                                        have look on example ......




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="center"
                                                                                        android:text="Center Layout Gravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#1b64b9"
                                                                                        android:gravity="bottom"
                                                                                        android:layout_gravity="bottom|center"
                                                                                        android:text="Bottom Layout Gravity" />

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:gravity="top"
                                                                                        android:layout_gravity="top|center"
                                                                                        android:text="Top Layout Gravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="100dp"
                                                                                        android:textColor="#d71f1c"
                                                                                        android:gravity="top|right"
                                                                                        android:layout_gravity="top|right"
                                                                                        android:text="Top Right Layout Gravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="bottom"
                                                                                        android:gravity="bottom"
                                                                                        android:text="Top Left Layout Gravity"/>

                                                                                        </FrameLayout>


                                                                                        Output:-



                                                                                        g1




                                                                                        Use Of Gravity and Layout-gravity in LinearLayout .....




                                                                                        Gravity working same as above but here differnce is that we can use Gravity inside the LinearLayout View and RelativeLayout View which is not possible in FrameLayout View.




                                                                                        LinearLayout with orientation vertical ....




                                                                                        Note:- Here we can set only 3 values of layout_gravity that is (left | right | center (also called center_horizontal)).




                                                                                        have look on example :-




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:orientation="vertical"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="center_horizontal"
                                                                                        android:text="Center Layout Gravity nor nCenter_Horizontal"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:layout_gravity="right"
                                                                                        android:text="Right Layout Gravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="left"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:gravity="bottom"
                                                                                        android:text="Left Layout Gravity"/>

                                                                                        </LinearLayout>


                                                                                        Output:-



                                                                                        g2




                                                                                        LinearLayout with orientation horizontal ....




                                                                                        Note:- Here we can set also 3 values of layout_gravity that is (top | bottom | center (also called center_vertical)).




                                                                                        have look on example :-




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:orientation="horizontal"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="120dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="bottom"
                                                                                        android:text="Bottom nLayout nGravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="120dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:layout_gravity="center"
                                                                                        android:text="Center nLayout nGravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="150dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="left"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:text="Left nLayout nGravity"/>

                                                                                        </LinearLayout>


                                                                                        output:-



                                                                                        g3



                                                                                        Note:- We can't use layout_gravity in the RelativeLayout Views but we can use gravity to set RelativeLayout childs to same position....






                                                                                        share|improve this answer


















                                                                                        • 1





                                                                                          If you set textColor white in all view then it would be very great. :D

                                                                                          – Pratik Butani AndroidDev
                                                                                          Feb 6 at 8:47











                                                                                        • Are you sure? It will look black and white that's why i used different color for every View.

                                                                                          – Sushil Kumar
                                                                                          Feb 6 at 9:54















                                                                                        6














                                                                                        There is many difference in the gravity and layout-gravity. I am going to explain my experience about these 2 concepts(All information i got due to my observation and some websites).




                                                                                        Use Of Gravity and Layout-gravity in FrameLayout .....




                                                                                        Note:-



                                                                                        1. Gravity is used inside the View Content as some User have answer and it is same for all ViewGroup Layout.


                                                                                        2. Layout-gravity is used with the parent View as some User have answer.


                                                                                        3. Gravity and Layout-gravity is work more useful with the FrameLayout childs . We can't use Gravity and Layout-gravity in FrameLayout's Tag ....


                                                                                        4. We can set Child View any where in the FrameLayout using layout-gravity .


                                                                                        5. We can use every single value of gravity inside the FrameLayout (eg:- center_vertical, center_horizontal, center,top, etc), but it is not possible with other ViewGroup Layouts .


                                                                                        6. FrameLayout fully working on Layout-gravity. Example:- If you work on FrameLayout then you don't need to change whole Layout for adding new View. You just add View as last in the FrameLayout and give him Layout-gravity with value.(This is adavantages of layout-gravity with FrameLayout).



                                                                                        have look on example ......




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="center"
                                                                                        android:text="Center Layout Gravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#1b64b9"
                                                                                        android:gravity="bottom"
                                                                                        android:layout_gravity="bottom|center"
                                                                                        android:text="Bottom Layout Gravity" />

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:gravity="top"
                                                                                        android:layout_gravity="top|center"
                                                                                        android:text="Top Layout Gravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="100dp"
                                                                                        android:textColor="#d71f1c"
                                                                                        android:gravity="top|right"
                                                                                        android:layout_gravity="top|right"
                                                                                        android:text="Top Right Layout Gravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="bottom"
                                                                                        android:gravity="bottom"
                                                                                        android:text="Top Left Layout Gravity"/>

                                                                                        </FrameLayout>


                                                                                        Output:-



                                                                                        g1




                                                                                        Use Of Gravity and Layout-gravity in LinearLayout .....




                                                                                        Gravity working same as above but here differnce is that we can use Gravity inside the LinearLayout View and RelativeLayout View which is not possible in FrameLayout View.




                                                                                        LinearLayout with orientation vertical ....




                                                                                        Note:- Here we can set only 3 values of layout_gravity that is (left | right | center (also called center_horizontal)).




                                                                                        have look on example :-




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:orientation="vertical"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="center_horizontal"
                                                                                        android:text="Center Layout Gravity nor nCenter_Horizontal"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:layout_gravity="right"
                                                                                        android:text="Right Layout Gravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="left"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:gravity="bottom"
                                                                                        android:text="Left Layout Gravity"/>

                                                                                        </LinearLayout>


                                                                                        Output:-



                                                                                        g2




                                                                                        LinearLayout with orientation horizontal ....




                                                                                        Note:- Here we can set also 3 values of layout_gravity that is (top | bottom | center (also called center_vertical)).




                                                                                        have look on example :-




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:orientation="horizontal"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="120dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="bottom"
                                                                                        android:text="Bottom nLayout nGravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="120dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:layout_gravity="center"
                                                                                        android:text="Center nLayout nGravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="150dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="left"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:text="Left nLayout nGravity"/>

                                                                                        </LinearLayout>


                                                                                        output:-



                                                                                        g3



                                                                                        Note:- We can't use layout_gravity in the RelativeLayout Views but we can use gravity to set RelativeLayout childs to same position....






                                                                                        share|improve this answer


















                                                                                        • 1





                                                                                          If you set textColor white in all view then it would be very great. :D

                                                                                          – Pratik Butani AndroidDev
                                                                                          Feb 6 at 8:47











                                                                                        • Are you sure? It will look black and white that's why i used different color for every View.

                                                                                          – Sushil Kumar
                                                                                          Feb 6 at 9:54













                                                                                        6












                                                                                        6








                                                                                        6







                                                                                        There is many difference in the gravity and layout-gravity. I am going to explain my experience about these 2 concepts(All information i got due to my observation and some websites).




                                                                                        Use Of Gravity and Layout-gravity in FrameLayout .....




                                                                                        Note:-



                                                                                        1. Gravity is used inside the View Content as some User have answer and it is same for all ViewGroup Layout.


                                                                                        2. Layout-gravity is used with the parent View as some User have answer.


                                                                                        3. Gravity and Layout-gravity is work more useful with the FrameLayout childs . We can't use Gravity and Layout-gravity in FrameLayout's Tag ....


                                                                                        4. We can set Child View any where in the FrameLayout using layout-gravity .


                                                                                        5. We can use every single value of gravity inside the FrameLayout (eg:- center_vertical, center_horizontal, center,top, etc), but it is not possible with other ViewGroup Layouts .


                                                                                        6. FrameLayout fully working on Layout-gravity. Example:- If you work on FrameLayout then you don't need to change whole Layout for adding new View. You just add View as last in the FrameLayout and give him Layout-gravity with value.(This is adavantages of layout-gravity with FrameLayout).



                                                                                        have look on example ......




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="center"
                                                                                        android:text="Center Layout Gravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#1b64b9"
                                                                                        android:gravity="bottom"
                                                                                        android:layout_gravity="bottom|center"
                                                                                        android:text="Bottom Layout Gravity" />

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:gravity="top"
                                                                                        android:layout_gravity="top|center"
                                                                                        android:text="Top Layout Gravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="100dp"
                                                                                        android:textColor="#d71f1c"
                                                                                        android:gravity="top|right"
                                                                                        android:layout_gravity="top|right"
                                                                                        android:text="Top Right Layout Gravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="bottom"
                                                                                        android:gravity="bottom"
                                                                                        android:text="Top Left Layout Gravity"/>

                                                                                        </FrameLayout>


                                                                                        Output:-



                                                                                        g1




                                                                                        Use Of Gravity and Layout-gravity in LinearLayout .....




                                                                                        Gravity working same as above but here differnce is that we can use Gravity inside the LinearLayout View and RelativeLayout View which is not possible in FrameLayout View.




                                                                                        LinearLayout with orientation vertical ....




                                                                                        Note:- Here we can set only 3 values of layout_gravity that is (left | right | center (also called center_horizontal)).




                                                                                        have look on example :-




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:orientation="vertical"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="center_horizontal"
                                                                                        android:text="Center Layout Gravity nor nCenter_Horizontal"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:layout_gravity="right"
                                                                                        android:text="Right Layout Gravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="left"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:gravity="bottom"
                                                                                        android:text="Left Layout Gravity"/>

                                                                                        </LinearLayout>


                                                                                        Output:-



                                                                                        g2




                                                                                        LinearLayout with orientation horizontal ....




                                                                                        Note:- Here we can set also 3 values of layout_gravity that is (top | bottom | center (also called center_vertical)).




                                                                                        have look on example :-




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:orientation="horizontal"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="120dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="bottom"
                                                                                        android:text="Bottom nLayout nGravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="120dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:layout_gravity="center"
                                                                                        android:text="Center nLayout nGravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="150dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="left"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:text="Left nLayout nGravity"/>

                                                                                        </LinearLayout>


                                                                                        output:-



                                                                                        g3



                                                                                        Note:- We can't use layout_gravity in the RelativeLayout Views but we can use gravity to set RelativeLayout childs to same position....






                                                                                        share|improve this answer













                                                                                        There is many difference in the gravity and layout-gravity. I am going to explain my experience about these 2 concepts(All information i got due to my observation and some websites).




                                                                                        Use Of Gravity and Layout-gravity in FrameLayout .....




                                                                                        Note:-



                                                                                        1. Gravity is used inside the View Content as some User have answer and it is same for all ViewGroup Layout.


                                                                                        2. Layout-gravity is used with the parent View as some User have answer.


                                                                                        3. Gravity and Layout-gravity is work more useful with the FrameLayout childs . We can't use Gravity and Layout-gravity in FrameLayout's Tag ....


                                                                                        4. We can set Child View any where in the FrameLayout using layout-gravity .


                                                                                        5. We can use every single value of gravity inside the FrameLayout (eg:- center_vertical, center_horizontal, center,top, etc), but it is not possible with other ViewGroup Layouts .


                                                                                        6. FrameLayout fully working on Layout-gravity. Example:- If you work on FrameLayout then you don't need to change whole Layout for adding new View. You just add View as last in the FrameLayout and give him Layout-gravity with value.(This is adavantages of layout-gravity with FrameLayout).



                                                                                        have look on example ......




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="center"
                                                                                        android:text="Center Layout Gravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#1b64b9"
                                                                                        android:gravity="bottom"
                                                                                        android:layout_gravity="bottom|center"
                                                                                        android:text="Bottom Layout Gravity" />

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:gravity="top"
                                                                                        android:layout_gravity="top|center"
                                                                                        android:text="Top Layout Gravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="100dp"
                                                                                        android:textColor="#d71f1c"
                                                                                        android:gravity="top|right"
                                                                                        android:layout_gravity="top|right"
                                                                                        android:text="Top Right Layout Gravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="bottom"
                                                                                        android:gravity="bottom"
                                                                                        android:text="Top Left Layout Gravity"/>

                                                                                        </FrameLayout>


                                                                                        Output:-



                                                                                        g1




                                                                                        Use Of Gravity and Layout-gravity in LinearLayout .....




                                                                                        Gravity working same as above but here differnce is that we can use Gravity inside the LinearLayout View and RelativeLayout View which is not possible in FrameLayout View.




                                                                                        LinearLayout with orientation vertical ....




                                                                                        Note:- Here we can set only 3 values of layout_gravity that is (left | right | center (also called center_horizontal)).




                                                                                        have look on example :-




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:orientation="vertical"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="center_horizontal"
                                                                                        android:text="Center Layout Gravity nor nCenter_Horizontal"/>

                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:layout_gravity="right"
                                                                                        android:text="Right Layout Gravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="80dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="left"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:gravity="bottom"
                                                                                        android:text="Left Layout Gravity"/>

                                                                                        </LinearLayout>


                                                                                        Output:-



                                                                                        g2




                                                                                        LinearLayout with orientation horizontal ....




                                                                                        Note:- Here we can set also 3 values of layout_gravity that is (top | bottom | center (also called center_vertical)).




                                                                                        have look on example :-




                                                                                        <?xml version="1.0" encoding="utf-8"?>
                                                                                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                                                        android:layout_width="match_parent"
                                                                                        android:orientation="horizontal"
                                                                                        android:layout_height="match_parent">

                                                                                        <TextView
                                                                                        android:layout_width="120dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:textColor="#264bd1"
                                                                                        android:gravity="center"
                                                                                        android:layout_gravity="bottom"
                                                                                        android:text="Bottom nLayout nGravity"/>

                                                                                        <TextView
                                                                                        android:layout_width="120dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:textColor="#d75d1c"
                                                                                        android:layout_gravity="center"
                                                                                        android:text="Center nLayout nGravity"/>


                                                                                        <TextView
                                                                                        android:layout_width="150dp"
                                                                                        android:layout_height="100dp"
                                                                                        android:textSize="25dp"
                                                                                        android:background="#000"
                                                                                        android:layout_marginBottom="100dp"
                                                                                        android:textColor="#d71cb2"
                                                                                        android:layout_gravity="left"
                                                                                        android:layout_marginTop="20dp"
                                                                                        android:text="Left nLayout nGravity"/>

                                                                                        </LinearLayout>


                                                                                        output:-



                                                                                        g3



                                                                                        Note:- We can't use layout_gravity in the RelativeLayout Views but we can use gravity to set RelativeLayout childs to same position....







                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered Oct 17 '16 at 6:48









                                                                                        Sushil KumarSushil Kumar

                                                                                        5,49431659




                                                                                        5,49431659







                                                                                        • 1





                                                                                          If you set textColor white in all view then it would be very great. :D

                                                                                          – Pratik Butani AndroidDev
                                                                                          Feb 6 at 8:47











                                                                                        • Are you sure? It will look black and white that's why i used different color for every View.

                                                                                          – Sushil Kumar
                                                                                          Feb 6 at 9:54












                                                                                        • 1





                                                                                          If you set textColor white in all view then it would be very great. :D

                                                                                          – Pratik Butani AndroidDev
                                                                                          Feb 6 at 8:47











                                                                                        • Are you sure? It will look black and white that's why i used different color for every View.

                                                                                          – Sushil Kumar
                                                                                          Feb 6 at 9:54







                                                                                        1




                                                                                        1





                                                                                        If you set textColor white in all view then it would be very great. :D

                                                                                        – Pratik Butani AndroidDev
                                                                                        Feb 6 at 8:47





                                                                                        If you set textColor white in all view then it would be very great. :D

                                                                                        – Pratik Butani AndroidDev
                                                                                        Feb 6 at 8:47













                                                                                        Are you sure? It will look black and white that's why i used different color for every View.

                                                                                        – Sushil Kumar
                                                                                        Feb 6 at 9:54





                                                                                        Are you sure? It will look black and white that's why i used different color for every View.

                                                                                        – Sushil Kumar
                                                                                        Feb 6 at 9:54











                                                                                        4














                                                                                        Something I saw on Sandip's blog that I almost missed, fixed my problem. He said layout_gravity DOES NOT WORK WITH LinearLayout.



                                                                                        If you're using a LinearLayout and the gravity settings are driving you nuts (like me), then switch to something else.



                                                                                        I actually switched to a RelativeLayout then used layout_alignParentLeft and layout_alignParentRight on the 2 contained TextViews to get them on one line to go far left and far right.






                                                                                        share|improve this answer





























                                                                                          4














                                                                                          Something I saw on Sandip's blog that I almost missed, fixed my problem. He said layout_gravity DOES NOT WORK WITH LinearLayout.



                                                                                          If you're using a LinearLayout and the gravity settings are driving you nuts (like me), then switch to something else.



                                                                                          I actually switched to a RelativeLayout then used layout_alignParentLeft and layout_alignParentRight on the 2 contained TextViews to get them on one line to go far left and far right.






                                                                                          share|improve this answer



























                                                                                            4












                                                                                            4








                                                                                            4







                                                                                            Something I saw on Sandip's blog that I almost missed, fixed my problem. He said layout_gravity DOES NOT WORK WITH LinearLayout.



                                                                                            If you're using a LinearLayout and the gravity settings are driving you nuts (like me), then switch to something else.



                                                                                            I actually switched to a RelativeLayout then used layout_alignParentLeft and layout_alignParentRight on the 2 contained TextViews to get them on one line to go far left and far right.






                                                                                            share|improve this answer















                                                                                            Something I saw on Sandip's blog that I almost missed, fixed my problem. He said layout_gravity DOES NOT WORK WITH LinearLayout.



                                                                                            If you're using a LinearLayout and the gravity settings are driving you nuts (like me), then switch to something else.



                                                                                            I actually switched to a RelativeLayout then used layout_alignParentLeft and layout_alignParentRight on the 2 contained TextViews to get them on one line to go far left and far right.







                                                                                            share|improve this answer














                                                                                            share|improve this answer



                                                                                            share|improve this answer








                                                                                            edited Apr 20 '15 at 15:51









                                                                                            Catalina

                                                                                            9781023




                                                                                            9781023










                                                                                            answered Jul 24 '14 at 19:34









                                                                                            JustSomeGuyJustSomeGuy

                                                                                            11815




                                                                                            11815





















                                                                                                4














                                                                                                An easy trick to remember this is gravity applies to us inside earth. So, android:gravity is for inside the view.



                                                                                                Rememeber the out in layout_gravity which would help you to remember that android:layout_gravity would refer to outside the view






                                                                                                share|improve this answer





























                                                                                                  4














                                                                                                  An easy trick to remember this is gravity applies to us inside earth. So, android:gravity is for inside the view.



                                                                                                  Rememeber the out in layout_gravity which would help you to remember that android:layout_gravity would refer to outside the view






                                                                                                  share|improve this answer



























                                                                                                    4












                                                                                                    4








                                                                                                    4







                                                                                                    An easy trick to remember this is gravity applies to us inside earth. So, android:gravity is for inside the view.



                                                                                                    Rememeber the out in layout_gravity which would help you to remember that android:layout_gravity would refer to outside the view






                                                                                                    share|improve this answer















                                                                                                    An easy trick to remember this is gravity applies to us inside earth. So, android:gravity is for inside the view.



                                                                                                    Rememeber the out in layout_gravity which would help you to remember that android:layout_gravity would refer to outside the view







                                                                                                    share|improve this answer














                                                                                                    share|improve this answer



                                                                                                    share|improve this answer








                                                                                                    edited Sep 24 '18 at 11:53

























                                                                                                    answered Nov 29 '16 at 8:41









                                                                                                    Mahendra LiyaMahendra Liya

                                                                                                    7,7591067101




                                                                                                    7,7591067101





















                                                                                                        2














                                                                                                        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




                                                                                                        http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html







                                                                                                        share|improve this answer


















                                                                                                        • 2





                                                                                                          I think no need of new answer as there are multiple answers already posted and with almost the same contents covered!

                                                                                                          – Paresh Mayani
                                                                                                          May 10 '15 at 13:56















                                                                                                        2














                                                                                                        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




                                                                                                        http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html







                                                                                                        share|improve this answer


















                                                                                                        • 2





                                                                                                          I think no need of new answer as there are multiple answers already posted and with almost the same contents covered!

                                                                                                          – Paresh Mayani
                                                                                                          May 10 '15 at 13:56













                                                                                                        2












                                                                                                        2








                                                                                                        2







                                                                                                        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




                                                                                                        http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html







                                                                                                        share|improve this answer













                                                                                                        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




                                                                                                        http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html








                                                                                                        share|improve this answer












                                                                                                        share|improve this answer



                                                                                                        share|improve this answer










                                                                                                        answered May 10 '15 at 12:17









                                                                                                        IntelliJ AmiyaIntelliJ Amiya

                                                                                                        53.8k12115138




                                                                                                        53.8k12115138







                                                                                                        • 2





                                                                                                          I think no need of new answer as there are multiple answers already posted and with almost the same contents covered!

                                                                                                          – Paresh Mayani
                                                                                                          May 10 '15 at 13:56












                                                                                                        • 2





                                                                                                          I think no need of new answer as there are multiple answers already posted and with almost the same contents covered!

                                                                                                          – Paresh Mayani
                                                                                                          May 10 '15 at 13:56







                                                                                                        2




                                                                                                        2





                                                                                                        I think no need of new answer as there are multiple answers already posted and with almost the same contents covered!

                                                                                                        – Paresh Mayani
                                                                                                        May 10 '15 at 13:56





                                                                                                        I think no need of new answer as there are multiple answers already posted and with almost the same contents covered!

                                                                                                        – Paresh Mayani
                                                                                                        May 10 '15 at 13:56











                                                                                                        2














                                                                                                        The basic difference between the two is that-



                                                                                                        android:gravity is used for child elements of the view.



                                                                                                        android:layout_gravity is used for this element with respect to parent view.






                                                                                                        share|improve this answer





























                                                                                                          2














                                                                                                          The basic difference between the two is that-



                                                                                                          android:gravity is used for child elements of the view.



                                                                                                          android:layout_gravity is used for this element with respect to parent view.






                                                                                                          share|improve this answer



























                                                                                                            2












                                                                                                            2








                                                                                                            2







                                                                                                            The basic difference between the two is that-



                                                                                                            android:gravity is used for child elements of the view.



                                                                                                            android:layout_gravity is used for this element with respect to parent view.






                                                                                                            share|improve this answer















                                                                                                            The basic difference between the two is that-



                                                                                                            android:gravity is used for child elements of the view.



                                                                                                            android:layout_gravity is used for this element with respect to parent view.







                                                                                                            share|improve this answer














                                                                                                            share|improve this answer



                                                                                                            share|improve this answer








                                                                                                            edited Sep 15 '16 at 15:23

























                                                                                                            answered Jul 26 '16 at 14:43









                                                                                                            Abhishek TandonAbhishek Tandon

                                                                                                            722826




                                                                                                            722826





















                                                                                                                2














                                                                                                                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)






                                                                                                                share|improve this answer



























                                                                                                                  2














                                                                                                                  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)






                                                                                                                  share|improve this answer

























                                                                                                                    2












                                                                                                                    2








                                                                                                                    2







                                                                                                                    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)






                                                                                                                    share|improve this answer













                                                                                                                    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)







                                                                                                                    share|improve this answer












                                                                                                                    share|improve this answer



                                                                                                                    share|improve this answer










                                                                                                                    answered Apr 30 '18 at 10:36









                                                                                                                    Ali RazaAli Raza

                                                                                                                    3113




                                                                                                                    3113





















                                                                                                                        1














                                                                                                                        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.






                                                                                                                        share|improve this answer



























                                                                                                                          1














                                                                                                                          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.






                                                                                                                          share|improve this answer

























                                                                                                                            1












                                                                                                                            1








                                                                                                                            1







                                                                                                                            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.






                                                                                                                            share|improve this answer













                                                                                                                            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.







                                                                                                                            share|improve this answer












                                                                                                                            share|improve this answer



                                                                                                                            share|improve this answer










                                                                                                                            answered May 17 '17 at 18:15









                                                                                                                            Praveen GaurPraveen Gaur

                                                                                                                            17615




                                                                                                                            17615





















                                                                                                                                1














                                                                                                                                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.






                                                                                                                                share|improve this answer

























                                                                                                                                • layout_gravity: is used for current view only gravity in context of it's relative parent, not other views which is inside of it's parent.

                                                                                                                                  – Chintak Patel
                                                                                                                                  Feb 7 '18 at 10:12















                                                                                                                                1














                                                                                                                                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.






                                                                                                                                share|improve this answer

























                                                                                                                                • layout_gravity: is used for current view only gravity in context of it's relative parent, not other views which is inside of it's parent.

                                                                                                                                  – Chintak Patel
                                                                                                                                  Feb 7 '18 at 10:12













                                                                                                                                1












                                                                                                                                1








                                                                                                                                1







                                                                                                                                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.






                                                                                                                                share|improve this answer















                                                                                                                                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.







                                                                                                                                share|improve this answer














                                                                                                                                share|improve this answer



                                                                                                                                share|improve this answer








                                                                                                                                edited Feb 7 '18 at 12:27









                                                                                                                                Chintak Patel

                                                                                                                                494420




                                                                                                                                494420










                                                                                                                                answered Jun 1 '17 at 11:32









                                                                                                                                Mayank GargMayank Garg

                                                                                                                                605417




                                                                                                                                605417












                                                                                                                                • layout_gravity: is used for current view only gravity in context of it's relative parent, not other views which is inside of it's parent.

                                                                                                                                  – Chintak Patel
                                                                                                                                  Feb 7 '18 at 10:12

















                                                                                                                                • layout_gravity: is used for current view only gravity in context of it's relative parent, not other views which is inside of it's parent.

                                                                                                                                  – Chintak Patel
                                                                                                                                  Feb 7 '18 at 10:12
















                                                                                                                                layout_gravity: is used for current view only gravity in context of it's relative parent, not other views which is inside of it's parent.

                                                                                                                                – Chintak Patel
                                                                                                                                Feb 7 '18 at 10:12





                                                                                                                                layout_gravity: is used for current view only gravity in context of it's relative parent, not other views which is inside of it's parent.

                                                                                                                                – Chintak Patel
                                                                                                                                Feb 7 '18 at 10:12











                                                                                                                                0














                                                                                                                                android:gravity


                                                                                                                                is used to adjust for content of the view relative to its specify position (allocated area). android:gravity="left" would not do anything if layout_width is equal to the "wrap_content"



                                                                                                                                android:layout_gravity 


                                                                                                                                is used for view itself relative to the parent or layout file.






                                                                                                                                share|improve this answer



























                                                                                                                                  0














                                                                                                                                  android:gravity


                                                                                                                                  is used to adjust for content of the view relative to its specify position (allocated area). android:gravity="left" would not do anything if layout_width is equal to the "wrap_content"



                                                                                                                                  android:layout_gravity 


                                                                                                                                  is used for view itself relative to the parent or layout file.






                                                                                                                                  share|improve this answer

























                                                                                                                                    0












                                                                                                                                    0








                                                                                                                                    0







                                                                                                                                    android:gravity


                                                                                                                                    is used to adjust for content of the view relative to its specify position (allocated area). android:gravity="left" would not do anything if layout_width is equal to the "wrap_content"



                                                                                                                                    android:layout_gravity 


                                                                                                                                    is used for view itself relative to the parent or layout file.






                                                                                                                                    share|improve this answer













                                                                                                                                    android:gravity


                                                                                                                                    is used to adjust for content of the view relative to its specify position (allocated area). android:gravity="left" would not do anything if layout_width is equal to the "wrap_content"



                                                                                                                                    android:layout_gravity 


                                                                                                                                    is used for view itself relative to the parent or layout file.







                                                                                                                                    share|improve this answer












                                                                                                                                    share|improve this answer



                                                                                                                                    share|improve this answer










                                                                                                                                    answered Jan 2 '17 at 12:13









                                                                                                                                    TulsiTulsi

                                                                                                                                    407413




                                                                                                                                    407413





















                                                                                                                                        0














                                                                                                                                        android:gravity -> Sets the gravity of the content of the View its used on.



                                                                                                                                        android:layout_gravity -> Sets the gravity of it's Parent's view or Layout






                                                                                                                                        share|improve this answer



























                                                                                                                                          0














                                                                                                                                          android:gravity -> Sets the gravity of the content of the View its used on.



                                                                                                                                          android:layout_gravity -> Sets the gravity of it's Parent's view or Layout






                                                                                                                                          share|improve this answer

























                                                                                                                                            0












                                                                                                                                            0








                                                                                                                                            0







                                                                                                                                            android:gravity -> Sets the gravity of the content of the View its used on.



                                                                                                                                            android:layout_gravity -> Sets the gravity of it's Parent's view or Layout






                                                                                                                                            share|improve this answer













                                                                                                                                            android:gravity -> Sets the gravity of the content of the View its used on.



                                                                                                                                            android:layout_gravity -> Sets the gravity of it's Parent's view or Layout







                                                                                                                                            share|improve this answer












                                                                                                                                            share|improve this answer



                                                                                                                                            share|improve this answer










                                                                                                                                            answered Jul 12 '17 at 17:10









                                                                                                                                            VamsiVamsi

                                                                                                                                            5721522




                                                                                                                                            5721522





















                                                                                                                                                0














                                                                                                                                                gravity--Applies to its own view.



                                                                                                                                                layout-gravity---Applies to view related to its parent.






                                                                                                                                                share|improve this answer



























                                                                                                                                                  0














                                                                                                                                                  gravity--Applies to its own view.



                                                                                                                                                  layout-gravity---Applies to view related to its parent.






                                                                                                                                                  share|improve this answer

























                                                                                                                                                    0












                                                                                                                                                    0








                                                                                                                                                    0







                                                                                                                                                    gravity--Applies to its own view.



                                                                                                                                                    layout-gravity---Applies to view related to its parent.






                                                                                                                                                    share|improve this answer













                                                                                                                                                    gravity--Applies to its own view.



                                                                                                                                                    layout-gravity---Applies to view related to its parent.







                                                                                                                                                    share|improve this answer












                                                                                                                                                    share|improve this answer



                                                                                                                                                    share|improve this answer










                                                                                                                                                    answered Nov 13 '18 at 10:07









                                                                                                                                                    nidhinidhi

                                                                                                                                                    248115




                                                                                                                                                    248115















                                                                                                                                                        protected by Community May 18 '13 at 4:07



                                                                                                                                                        Thank you for your interest in this question.
                                                                                                                                                        Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                                                                        Would you like to answer one of these unanswered questions instead?



                                                                                                                                                        Popular posts from this blog

                                                                                                                                                        𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                                                                                                                                        Edmonton

                                                                                                                                                        Crossroads (UK TV series)