2

I have done some basic xml layout from the Internet and I am confused as to what android:gravity would be used for vs android:layout-gravity. The UI designer seems to respond well when I change the layout_gravity, but I get no response to the regular gravity? What is up with this?

Is this similar to layout parameters with fill-parent and match-parent? one is gone now.

  • This question is duplicate to [post][1] please go through it [1]: http://stackoverflow.com/questions/3482742/android-gravity-and-layout-gravity – Raghuram Sep 26 '13 at 12:41
  • Their names should help you : android:gravity sets the gravity of the content of the View its used on. android:layout_gravity sets the gravity of the View or Layout in its parent. http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/ – Amit Sep 26 '13 at 12:41

2 Answers2

3

android:gravity is a statement from a parent to its content, indicating where the content should slide within the available parent space (if there is room for such sliding).

android:layout_gravity is a request from a child to its parent, asking that it be slid in a certain direction (if there is room for such sliding).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

layout_gravity - It contains and it belongs to component's parent layout. Like if you set one TextView in LinearLayout, LinearLayout's width is fill_parent, but TextView basically will set at left-top. So now if you would give layout_gravity=right then your TextView will be shifted to top-right corner. But the text will not change its place. i.e. you had been wrote "Hello world" then "hello world" will not be changed to right, but its parent textView will be changed to right. So here in this case it will belongs to LinearLayout, means parent Layout.

gravity- It contains and it belongs to TextView as per above explanation. if you will give gravity="right" then "Hello-world" text will go to right-top corner of TextView not LinearLayout.

Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44