{"id":4294,"date":"2020-07-21T21:30:22","date_gmt":"2020-07-21T11:30:22","guid":{"rendered":"https:\/\/learntodroid.com\/?p=4294"},"modified":"2020-08-13T23:24:52","modified_gmt":"2020-08-13T13:24:52","slug":"what-is-the-difference-between-gravity-and-layout_gravity-in-android","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/what-is-the-difference-between-gravity-and-layout_gravity-in-android\/","title":{"rendered":"What Is the Difference Between Gravity and Layout_Gravity in Android?"},"content":{"rendered":"\n
In the user interface of your Android app you can use gravity and layout_gravity attributes to align widgets in your layout but sometimes it can be difficult to remember which attribute to use.<\/p>\n\n\n\n
The difference between the gravity and layout_gravity XML attributes in Android is that the android:gravity attribute is used to arrange the position of the content inside a View (for example text inside a Button widget) while the android:layout_gravity is used to arrange the position of the entire View relative to it’s container.<\/strong><\/p>\n\n\n\n In this post I have documented the possible values for the layout_gravity and gravity attributes in Android. Also I have included code samples and screenshots showing how to use the layout_gravity and gravity XML attributes for the TextView, Button and EditText widgets.<\/p>\n\n\n\n I have also added section on how to set the gravity of views programmatically using the setGravity(…) method.<\/p>\n\n\n\n The code samples shared in this blog post are also available in a public GitHub repository I created at the link below.<\/p>\n\n\n\n