1. Are you ready for the Galaxy S20? Here is everything we know so far!

Difference between layout_gravity and gravity

Discussion in 'Android Development' started by raduq, Sep 24, 2011.

  1. raduq

    raduq Newbie
    Thread Starter

    Hello!

    I have just started about two weeks ago to develop Android applications and also, just started working with XML. I think I understood most of the things in XML and the way it influences the application, but I can't understand the difference between android:layout_gravity and android:gravity, applied for example on a button.

    I have tried to align a button to the center and used this code:

    Code (Text):
    1. <Button
    2.            android:id="@+id/login_button"
    3.            android:layout_width="wrap_content"
    4.            android:layout_height="wrap_content"
    5.            android:text="Log in"
    6.            android:layout_gravity="center" />
    and it worked properly. But also, I have tried the one where "android:layout_gravity" is replaced with the simple "android:gravity" with the same center option, but it does not work.

    I have read in the small pop-up box with the options that appears when I type in the code that the "layout_gravity" is "Standard gravity constant that a child can supply to its parent."

    So, the child in my situation is the <Button /> and the parent is the <LinearLayout />, right?

    If that's right, it means that the layout_gravity:center in the <Button />tag influences the <LinearLayout /> tag? I'm almost sure that's not the deal, but what's the thing with CHILD and PARENT? I got it a bit mixed up.. as I think you can see already.

    Any help is very much appreciated!
     


  2. jonbonazza

    jonbonazza Android Expert

    android:gravity applies graivity to the content of the view, while
    android:layout_gravity applies gravity to the view itself inside its parent.
     
    CreatorCool likes this.
Loading...
Similar Threads - Difference between layout_gravity
  1. RIdge de Waal
    Replies:
    4
    Views:
    225
  2. Affydingus
    Replies:
    2
    Views:
    519
  3. MoodyBlues
    Replies:
    12
    Views:
    789
  4. Daniel Fernandes
    Replies:
    0
    Views:
    375
  5. James_Watson
    Replies:
    0
    Views:
    2,084
  6. oldun
    Replies:
    5
    Views:
    418
  7. James_Watson
    Replies:
    1
    Views:
    611
  8. mackattack777
    Replies:
    0
    Views:
    1,209
  9. Abhinv
    Replies:
    0
    Views:
    1,403
  10. a cooperator
    Replies:
    2
    Views:
    714

Share This Page

Loading...