{"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

https:\/\/github.com\/learntodroid\/AndroidGravityLayoutGravity<\/a><\/p>\n\n\n\n

What are the possible values for the layout_gravity attribute in Android?<\/h2>\n\n\n\n

I have created the table below summarising the different attribute values available for layout_gravity in Android.<\/p>\n\n\n\n

Attribute Value for “layout_gravity”<\/th>Description<\/th><\/tr><\/thead>
left<\/td>Position the view to the left of it’s container<\/td><\/tr>
right<\/td>Position the view to the right of it’s container<\/td><\/tr>
start<\/td>Position the view to the beginning of it’s container<\/td><\/tr>
end<\/td>Position the view to the end of it’s container<\/td><\/tr>
top<\/td>Position the view to the top of it’s container<\/td><\/tr>
bottom<\/td>Position the view to the bottom of it’s container<\/td><\/tr>
center<\/td>Position the view to the center of it’s container in both the vertical and horizontal directions<\/td><\/tr>
center_horizontal<\/td>Position the view to the center of it’s container in only the horizontal direction<\/td><\/tr>
center_vertical<\/td>Position the view to the center of it’s container in only the vertical direction<\/td><\/tr>
fill<\/td>Scale the view in the horizonal and vertical direction so that it fills it’s container<\/td><\/tr>
fill_horizontal<\/td>Scale the view in only the horizontal direction so that it fills it’s container<\/td><\/tr>
fill_vertical<\/td>Scale the view in only the vertical direction so that it fills it’s container<\/td><\/tr>
clip_horizontal<\/td>Clip the left and\/or right side(s) of the view within it’s container<\/td><\/tr>
clip_vertical<\/td>Clip the top and\/or bottom side(s) of the view within it’s container<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

Multiple attribute values can be used together when setting the attribute value for android:layout_gravity by using the pipe “|” delimiter.<\/p>\n\n\n\n

For example the following screenshot shows a TextView inside a LinearLayout with the android:layout_gravity attribute on the TextView set to “top|right”.<\/p>\n\n\n\n

\"\"<\/figure><\/div>\n\n\n\n

See the code sample for the layout resource file below.<\/p>\n\n\n