What is android layout gravity

What Is the Difference Between Gravity and Layout_Gravity in Android?

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.

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.

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.

I have also added section on how to set the gravity of views programmatically using the setGravity(…) method.

The code samples shared in this blog post are also available in a public GitHub repository I created at the link below.

What are the possible values for the layout_gravity attribute in Android?

I have created the table below summarising the different attribute values available for layout_gravity in Android.

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

Multiple attribute values can be used together when setting the attribute value for android:layout_gravity by using the pipe “|” delimiter.

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

See the code sample for the layout resource file below.

What are the possible values for gravity attribute in Android?

I have created the table below summarising the different attribute values available for gravity in Android.

Attribute Value for “gravity” Description
left Position the content inside the view to the left
right Position the content inside the view to the right
start Position the content inside the view to the start
end Position the content inside the view to the end
top Position the content inside the view to the top
bottom Position the content inside the view to the bottom
center Position the content inside the view to the center in both the vertical and horizontal directions
center_horizontal Position the content inside the view to the center in only the horizontal direction
center_vertical Position the content inside the view to the center only the vertical direction
fill Scale the content inside the view in the horizonal and vertical direction so that it fills it’s container
fill_horizontal Scale the content inside the view in only the horizontal direction so that it fills it’s container
fill_vertical Scale the content inside the view in only the vertical direction so that it fills it’s container
clip_horizontal Clip the left and/or right side(s) of the content inside the view
clip_vertical Clip the top and/or button side(s) of the content inside the view

Multiple attribute values can be used together when setting the attribute value for android:gravity by using the pipe “|” delimiter.

For example the following screenshot shows a TextView inside a LinearLayout with the android:gravity attribute on the TextView set to “bottom|right”.

See the code sample for the layout resource file below.

How to use layout_gravity and gravity with a TextView in Android

When using a TextView widget in an Android app you can use the layout_gravity and gravity attributes to change the look and feel of the TextView.

The layout_gravity attribute can be used to position the TextView as a whole. The gravity attribute can be used to position the text inside the TextView.

See the screenshot below showing the use of the layout_gravity attribute and the gravity attribute for the TextView widget.

The screenshot above is built based on the layout resource below.

How to use layout_gravity and gravity with a Button in Android

When using a Button widget in an Android app you can use the layout_gravity and gravity attributes to change the look and feel of the Button.

The layout_gravity attribute can be used to position the Button as a whole with the Button text centered in side it. The gravity attribute can be used to position the text inside the Button.

See the screenshot below showing the use of the layout_gravity attribute and the gravity attribute for the Button widget.

The screenshot above is built based on the layout resource below.

How to use layout_gravity and gravity with an EditText in Android

When using a EditText widget in an Android app you can use the layout_gravity and gravity attributes to change the look and feel of the EditText.

The layout_gravity attribute can be used to position the EditText as a whole. The gravity attribute can be used to position the EditText hint inside the EditText.

See the screenshot below showing the use of the layout_gravity attribute and the gravity attribute for the EditText widget.

The screenshot above is built based on the layout resource below.

How to set gravity programmatically in Android

You can also set the gravity for a View programmatically in Android using the setGravity(…) method.

The setGravity(…) method accepts an integer as a parameter which can be one of the constants available on the following page in the official docs for gravity.

See the code sample below setting the gravity on a TextView using the setGravity(…) method.

The code sample above for setting the gravity on a TextView widget programmatically will produce the following screenshot.

Источник

Android Developer Beginner. FAQ #2 — gravity vs layout_gravity

This time I’ll focus on differences between android:layout_gravity and android:gravity attributes. Using these two can be confusing especially when we’re using them in a way that is not allowed.

layout_gravity

Let’s create a simple layout with one TextView.

The FrameLayout is a very simple one and it isn’t the best choice to design complicated layout because we don’t have much possibilities to control and place it’s child views. However we’re able to do this by using android:layout_gravity on the views inside the layout.

So now we’ll add this attribute the our TextView.

As you can see, the TextView is centered. Now let’s add another view in the right-bottom corner of the layout.

To apply more the one gravity value we simply just have to separate them with the | character.

If child views are using this attribute to tell their parent how they want to be placed, does it mean that every parent respects that? Well, no. We have different types of layouts. One of them is FrameLayout, but there are also LinearLayout, RelativeLayout and ConstraintLayout. The last one we’ll skip, because it’s totally different then the other ones.

Let’s for example take the vertical LinearLayout with one centered TextView.

The LinearLayout treats this attribute in a different way. Inside the FrameLayout views can overlap each other, but not inside the LinearLayout — that one places one view next to the other one. In our case orientation is set to vertical, that means that next view will be placed below the TextView. So as a child of this layout, only what we can ask our parent is: “Ok, I get that the list is vertical so I can’t be centered vertically, but please at least center me horizontally”. Let’s add more views.

I guess that now we can see the case more clearly. There are three TextViews inside the LinearLayout and all of them want to be centered, so which one should be displayed in that way? This is probably the reason that this attribute in this layout is handling just the opposite axis (x or y) than the chosen orientation, not both of them like the FrameLayout. You should check if that’s true, and try the horizontal orientation.

Ok, so what is happening inside the RelativeLayout? Let’s find out!

Nothing, it doesn’t respects this attribute. Generally, this layout makes possible to set relations between some views. For example a title TextView and a subtitle TextView should be on the right of the avatar but also the subtitle TextView should be below the title and so forth.

However in case we want to center some view in this layout we should use android:layout_centerInParent attribute and set it to true.

Now it’s working. The result is the same as inside the FrameLayout — the view is centered vertically and horizontally. If we want to place some view on the right side or on the bottom (like we did on the FrameLayout), we should use attributes like android:layout_alignParentEnd or android:layout_alignParentBottom and so on.

Ok, so what about android:gravity attribute?

gravity

In above examples we’ve described how android:layout_gravity is respected by different layouts. Thanks to that attribute the child views can somehow communicate with the parent layout and tell how they want to be placed inside it — different layouts respect it in a different way, but in general this is how it’s working.

What about communication in the opposite direction — from parent to child views? For that we’ll need android:gravity attribute.

Let’s start from the RelativeLayout.

We’ve set the center value into gravity attribute on the RelativeLayout and the TextView is centered. Ok, let’s add another view.

The Button is centered vertically but not exactly horizontally. Actually the top-left corner of the button is placed in the top-left corner of the TextView. If we change the height of the TextView we’ll see it more clearly.

Ok, but why it’s working like that? Is it some kind of a bug? Nope. We can find the explanation in the documentation of setGravity() method of the RelativeLayout:

Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. This happens after children have been relatively positioned.

It means, that all child views inside the RelativeLayout are treated by the gravity attribute as a one child. It takes the maximum hight from all of the child views and the maximum width, centered the unit with these bounds and place all of the views inside it starting with the top-left corner. We can change the Button’s height to check it.

We have to be careful with using this attribute if we want to apply it for more than one view inside this layout.

What about FrameLayout?

As we can see, FrameLayout doesn’t respect this attribute at all. When we look at the documentation for this layout we’ll find out that setGravity() method isn’t mentioned there.

The last one is LinearLayout. Let’s create one with the vertical orientation.

Cool, TextView is centered. What about more than one view?

The next view is placed below the first one, but both of them in general are centered. I guess that I use often that kind of approach to create layouts like login screen or similar.

One more example with horizontal orientation.

Ok, we can say that this layout places views next to each other (with the chosen orientations) starting from the point specified by values included in gravity attribute.

So in general android:layout_gravity attribute is used by child views to tell their parent how they want to be placed inside it, while android:gravity is used by the parent layout to tell the child views how they should be placed inside it.

The first one we should apply to the views inside some layout, however the second one we can use not only as an attribute in the layout. Let’s look at example.

There is some TextView with match_parent width and 100dp height. The text inside the TextView is started from the top-left corner. How to center it? With the use of android:gravity attribute.

Cool, now it looks much better. We can think about the text inside the TextView as a child view and the TextView as a parent tells the text how it should be placed.

Ok. I guess that’s it! Hope you enjoyed it. Take care.

Источник

Читайте также:  Гугл земля для андроид стабильная версия
Оцените статью